我有一个查询要在表中插入一行,该表有一个名为 ID 的字段,该字段是使用列上的 AUTO_INCRMENT 填充的。我需要为下一个功能获取这个值,但是当我运行以下命令时,它总是返回 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。有任何想法吗?
EDIT:
当我跑步时:
"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();"
I get:
{"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"}