【发布时间】: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