列に AUTO_INCREMENT を使用して入力される ID というフィールドがあるテーブルに行を挿入するクエリがあります。次の機能のためにこの値を取得する必要がありますが、以下を実行すると、実際の値が 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"}