我有一個查詢將行插入到表中,該表有一個名爲ID的字段,該字段在列上使用AUTO_INCREMENT填充。我需要的功能下一位該值,但是當我運行下面的,它總是返回0即使實際值不爲0:使用C#獲取插入行的ID
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"}
1.你能張貼被執行最後的CommandText? 2。記錄是否被插入? – 2009-01-02 03:20:19
我發佈了查詢,錯誤,並且是,正在插入行。 – Elie 2009-01-02 03:24:50
好的,如何「SELECT last_insert_id();」最後? – 2009-01-02 03:28:07