简体   繁体   English

如何获得LAST_INSERT_ID();

[英]How to get LAST_INSERT_ID();

I have looked around and I am still confused on how to get the last inserted ID. 我环顾四周,但仍然对如何获取最后插入的ID感到困惑。 I added the statment SELECT LAST_INSERT_ID(); 我添加了语句SELECT LAST_INSERT_ID(); at the end of mysql statement i am executing. 在我正在执行的mysql语句的末尾。 I am storing the value in prID = Convert.ToInt32(cmd.ExecuteScalar()); 我将值存储在prID = Convert.ToInt32(cmd.ExecuteScalar()); This command is creating two instances in my database. 该命令正在我的数据库中创建两个实例。 I am pretty sure I need to separate these two statements but unsure how to while still getting the last ID. 我很确定我需要将这两个语句分开,但是不确定如何在仍获得最后一个ID的同时。

          try
            {
                Console.WriteLine("Connecting to MySQL...");
                conn.Open();
                string sql = "INSERT INTO pull_requests (repoID, branchID, fileID, prStatus, prComments) VALUES (@rID, @bID, @fID, @prS, @prC); SELECT LAST_INSERT_ID();";
                MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@rID", RI);
                cmd.Parameters.AddWithValue("@bID", BI);
                cmd.Parameters.AddWithValue("@fID", FI);
                cmd.Parameters.AddWithValue("@prS", 0);
                cmd.Parameters.AddWithValue("@prC", comment);
                prID = Convert.ToInt32(cmd.ExecuteScalar());

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            conn.Close();

You need to only call cmd.ExecuteNonQuery() to execute the insert statement. 您只需要调用cmd.ExecuteNonQuery()即可执行插入语句。 On the return, the cmd object will have its .LastInsertedId property populated for you. 返回时,将为您填充cmd对象的.LastInsertedId属性。

Like this: 像这样:

try
{
    Console.WriteLine("Connecting to MySQL...");
    conn.Open();
    string sql = "INSERT INTO pull_requests (repoID, branchID, fileID, prStatus, prComments) VALUES (@rID, @bID, @fID, @prS, @prC);";
    MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn);
    cmd.Parameters.AddWithValue("@rID", RI);
    cmd.Parameters.AddWithValue("@bID", BI);
    cmd.Parameters.AddWithValue("@fID", FI);
    cmd.Parameters.AddWithValue("@prS", 0);
    cmd.Parameters.AddWithValue("@prC", comment);
    cmd.ExecuteNonQuery();

    long lastId = cmd.LastInsertedId;
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}
conn.Close();

执行查询后,使用MySqlCommand.LastInsertedId属性。

Typically, the SELECT last_insert_id() is executed separately, immediately after the INSERT is executed, on the same connection. 通常,在执行INSERT之后,立即在同一连接上分别执行SELECT last_insert_id()。 The result of last_insert_id() is connection specific, so you do not need to worry about other clients "overwriting" yours. last_insert_id()的结果是特定于连接的,因此您不必担心其他客户端“覆盖”您的客户端。

You can even reuse the same command with just cmd.CommandText = "SELECT last_insert_id()"; 您甚至可以只使用cmd.CommandText = "SELECT last_insert_id()";重用同一命令cmd.CommandText = "SELECT last_insert_id()";

...but as others have pointed out, and a quick web search has clarified for me, it looks like the MySQL .Net connector you are using already provides that without a second query. ...但是正如其他人指出的那样,快速的网络搜索为我澄清了,您正在使用的MySQL .Net连接器似乎已经提供了该功能,而无需第二次查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM