【问题标题】:C# Oracle OracleDataReader.Read()C# Oracle OracleDataReader.Read()
【发布时间】:2015-10-22 12:40:15
【问题描述】:

我的 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;
    }

【问题讨论】:

  • 你有没有调试过你的代码并看到你的dr真的 2行?
  • 未提交的事务?
  • dr 返回 0 行。这就是问题所在。
  • @Guido 你确定你的.._ID 列是字符类型的吗?看起来他们应该是基于他们的名字的数字。
  • 尝试从 SQL 中删除 WHERE 并查看表是否包含任何记录。如果是,请检查WHERE

标签: c# database oracle plsql


【解决方案1】:

发现错误,

数据库的 CREATE 脚本中没有提交语句。 放入后就可以了。

【讨论】:

  • 这很令人困惑,那么它是如何使用 sqldeveloper 工作的,您只能在给定会话中看到未提交的数据,否则对于所有其他会话,除非提交,否则它不存在
  • 这是事务与数据库无关的行为,所以新学习:)
猜你喜欢
  • 1970-01-01
  • 2013-05-12
  • 2013-11-26
  • 2011-09-26
  • 1970-01-01
  • 2015-08-23
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多