C#Oracle OracleDataReader.Read()

时间:2015-10-22 12:40:15

标签: c# database oracle plsql

我的Oracle和C#遇到了一些问题。当我运行此查询时,它返回0行,因此rd.Read()= false。但这很奇怪,因为数据库中有记录。在SQL Developer中运行完全相同的查询返回2行。

我正在运行Oracle XE 11.2

任何人都可以解释为什么dr.Read()返回false?

编辑:连接到数据库的工作率为100%。

    public List<ChatMessage> ReceiveMessage(string account_ID, string account_ID2)
    {
        //Initialize list of strings for all the messages
        List<ChatMessage> list = new List<ChatMessage>();

        //Make connection
        OracleConnection con = new OracleConnection(connstring);
        con.Open();

        //Make query
        string query = "SELECT Sender_ID, Receiver_ID, Message, Msg_Date FROM TBL_CHAT WHERE Sender_ID = '1' AND Receiver_ID = '2' OR Sender_ID = '2' AND Receiver_ID = '1' ORDER BY Msg_Date DESC";

        //Add parameters
        OracleCommand cmd = new OracleCommand();
        //cmd.Parameters.Add("account_ID", account_ID);
        //cmd.Parameters.Add("account_ID2", account_ID2);

        cmd.Connection = con;
        cmd.CommandText = query;

        //Execute query and initialize reader
        OracleDataReader dr = cmd.ExecuteReader();

        //Start reading the messages and add them to a list
        while(dr.Read())
        {
            string sender_ID = dr.GetString(0);
            string receiver_ID = dr.GetString(1);
            string message = dr.GetString(2);
            DateTime date = dr.GetDateTime(3);

            ChatMessage msg = new ChatMessage(message, sender_ID, receiver_ID, date);
            list.Add(msg);
        }

        //Close database
        con.Close();

        return list;
    }

1 个答案:

答案 0 :(得分:0)

发现错误,

数据库的CREATE脚本中没有commit语句。 把它放进去后就可以了。