所以我有以下代码(大大简化)-
MySqlCommand CMD = new MySqlCommand(@"INSERT INTO helpdesk.calls Set CallID = @CallID, Viewed = 1, Date = current_timestamp();
SELECT * FROM helpdesk.calls WHERE ID = @CallID", Con);
CMD.Parameters.Add("CallID", MySqlDbType.Int32).Value = ID;
using (MySqlDataReader Reader = CMD.ExecuteReader()) {
if (Reader.Read()) {
RetVal = ToCallHeader(Reader);
} else {
// this never throws as Reader.Read() always returns true.
throw new Exception(string.Format("Call ({0}) not found", ID));
}
}
我在其中插入一条记录并同时运行一个选择。问题在于,当使用 insert 语句时,无论是否从 select 语句中选择任何行,Reader.Read() 都会返回 true。这是设计使然吗?一个事务是否需要两个查询?
最佳答案
插入查询语法错误,会抛出异常。
INSERT INTO helpdesk.calls Set CallID = @CallID, Viewed = 1, Date = current_timestamp();
在此处检查插入语句的正确 SQL 语法:https://www.w3schools.com/sql/sql_insert.asp
关于c# - 当不存在时,同一语句中的 MySQL Insert 和 Select 返回 'records',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45751014/