gpt4 book ai didi

c# - 使用 C# 获取插入行的 id

转载 作者:IT老高 更新时间:2023-10-28 23:49:17 25 4
gpt4 key购买 nike

我有一个查询要将一行插入到表中,该表有一个名为 ID 的字段,该字段是使用列上的 AUTO_INCREMENT 填充的。我需要为下一个功能获取此值,但是当我运行以下命令时,它总是返回 0,即使实际值不是 0:

MySqlCommand comm = connect.CreateCommand();
comm.CommandText = insertInvoice;
comm.CommandText += "\'" + invoiceDate.ToString("yyyy:MM:dd hh:mm:ss") + "\', " + bookFee + ", " + adminFee + ", " + totalFee + ", " + customerID + ")";
int id = Convert.ToInt32(comm.ExecuteScalar());

根据我的理解,这应该返回 ID 列,但它每次都返回 0。有什么想法吗?

编辑:

当我运行时:

"INSERT INTO INVOICE (INVOICE_DATE, BOOK_FEE, ADMIN_FEE, TOTAL_FEE, CUSTOMER_ID) VALUES ('2009:01:01 10:21:12', 50, 7, 57, 2134);last_insert_id();"

我明白了:

{"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'last_insert_id()' at line 1"}

最佳答案

MySqlCommand comm = connect.CreateCommand();
comm.CommandText = insertStatement; // Set the insert statement
comm.ExecuteNonQuery(); // Execute the command
long id = comm.LastInsertedId; // Get the ID of the inserted item

关于c# - 使用 C# 获取插入行的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/405910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com