MySQL Insert&当不存在时,在同一语句中选择返回'记录'

时间:2017-08-18 07:34:19

标签: c# mysql sql

所以我有以下代码(大大简化) -

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));
    }
}

我在哪里插入记录并同时运行select。问题是当使用insert语句时,Reader.Read()返回true是否从select语句中选择任何行。这是行为设计​​,我是否需要对事务进行两次查询?

1 个答案:

答案 0 :(得分:0)

插入查询语法错误,它将引发异常。

INSERT INTO helpdesk.calls Set CallID = @CallID, Viewed = 1, Date = current_timestamp();

在此处检查插入语句中的正确sql语法:https://www.w3schools.com/sql/sql_insert.asp