开发者社区> 问答> 正文

使用C#获取插入行的ID?mysql

我有一个查询要在表中插入一行,该表有一个名为ID的字段,该字段在列上使用AUTO_INCREMENT填充。我需要为下一个功能获取此值,但是当我运行以下命令时,即使实际值不为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

展开
收起
保持可爱mmm 2020-05-17 22:34:23 30695 0
1 条回答
写回答
取消 提交回答
  • 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());来源:stack overflow

    2020-05-17 22:34:35
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像