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
[编辑:在引用last_insert_id()之前添加"select"]
select last_insert_id();
插入后运行" " 怎么样?
MySqlCommand comm = connect.CreateCommand(); comm.CommandText = insertInvoice; comm.CommandText += "\'" + invoiceDate.ToString("yyyy:MM:dd hh:mm:ss") + "\', " + bookFee + ", " + adminFee + ", " + totalFee + ", " + customerID + ");"; + "select last_insert_id();" int id = Convert.ToInt32(comm.ExecuteScalar());
编辑:正如duffymo所提到的,使用像这样的参数化查询你会得到很好的服务.
编辑:直到你切换到参数化版本,你可能会发现与string.Format和平:
comm.CommandText = string.Format("{0} '{1}', {2}, {3}, {4}, {5}); select last_insert_id();", insertInvoice, invoiceDate.ToString(...), bookFee, adminFee, totalFee, customerID);