使用C#获取插入行的id
我有一个查询要在表中插入一行,该表有一个名为 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每次。 有任何想法吗?
编辑:
当我跑步时:
"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"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
[编辑:在引用last_insert_id()之前添加“select”]
插入后运行“
select last_insert_id();
”怎么样?编辑:正如duffymo提到的,使用参数化查询确实会得到很好的服务像这样。
编辑:在切换到参数化版本之前,您可能会发现 string.Format 很平静:
[Edit: added "select" before references to last_insert_id()]
What about running "
select last_insert_id();
" after your insert?Edit: As duffymo mentioned, you really would be well served using parameterized queries like this.
Edit: Until you switch over to a parameterized version, you might find peace with string.Format:
使用最后插入的 ID。
查看我的建议和示例: http://livshitz.wordpress.com/2011/10/28/returning-last-inserted-id-in-c-using-mysql-db-provider/
Use LastInsertedId.
View my suggestion with example here: http://livshitz.wordpress.com/2011/10/28/returning-last-inserted-id-in-c-using-mysql-db-provider/
看到有人获取日期并将其作为字符串存储在数据库中,我感到很困扰。 为什么不让列类型反映现实呢?
我还惊讶地发现 SQL 查询是使用字符串连接构建的。 我是一名 Java 开发人员,我根本不懂 C#,但我想知道库中的某个地方是否没有类似于 java.sql.PreparedStatement 的绑定机制? 建议用于防范 SQL 注入攻击。 另一个好处是可能带来的性能好处,因为 SQL 可以被解析、验证、缓存一次并重用。
It bothers me to see anybody taking a Date and storing it in a database as a String. Why not have the column type reflect reality?
I'm also surprised to see a SQL query being built up using string concatenation. I'm a Java developer, and I don't know C# at all, but I'd wonder if there wasn't a binding mechanism along the lines of java.sql.PreparedStatement somewhere in the library? It's recommended for guarding against SQL injection attacks. Another benefit is possible performance benefits, because the SQL can be parsed, verified, cached once, and reused.
实际上,ExecuteScalar 方法返回所返回的 DataSet 的第一行的第一列。 就您而言,您只是执行插入操作,实际上并未查询任何数据。 插入后您需要查询scope_identity()(这是SQL Server 的语法),然后您就会得到答案。 请参阅此处:
链接
编辑:正如Michael Haren指出的,您在标签中提到您正在使用MySql,请使用last_insert_id(); 而不是scope_identity();
Actually, the ExecuteScalar method returns the first column of the first row of the DataSet being returned. In your case, you're only doing an Insert, you're not actually querying any data. You need to query the scope_identity() after you're insert (that's the syntax for SQL Server) and then you'll have your answer. See here:
Linkage
EDIT: As Michael Haren pointed out, you mentioned in your tag you're using MySql, use last_insert_id(); instead of scope_identity();