我无法弄清楚如何LAST_INSERT_ID()
在 WebMatrix 中使用 MySQL 数据库。有人可以告诉我如何将它应用于下面的代码片段吗?
var clothingsize="";
if(IsPost){
clothingsize =Request["clothingsize"];
var SQLINSERT = "INSERT INTO fit1 (clothingsize) VALUES (@0)"; }
我无法弄清楚如何LAST_INSERT_ID()
在 WebMatrix 中使用 MySQL 数据库。有人可以告诉我如何将它应用于下面的代码片段吗?
var clothingsize="";
if(IsPost){
clothingsize =Request["clothingsize"];
var SQLINSERT = "INSERT INTO fit1 (clothingsize) VALUES (@0)"; }
我可以假设您使用的是 MySQL .NET 连接器吗?
http://dev.mysql.com/downloads/connector/net
如果是这样,这是您正在寻找的一个非常简单的示例:
http://forums.mysql.com/read.php?38,98672,98868#msg-98868
执行插入命令后(C#,连接存储在 conDBConnection 中):
> string strSQLSelect = "SELECT @@IDENTITY AS 'LastID'";
> MySqlCommand dbcSelect = new MySqlCommand(strSQLSelect,
> conDBConnection); MySqlDataReader dbrSelect =
> dbcSelect.ExecuteReader();
>
> dbrSelect.Read(); int intCounter =
> Int32.Parse(dbrSelect.GetValue(0).ToString());
>
> dbrSelect.Dispose(); conDBConnection.Close();
快乐编码!
这是有效的解决方案:
在注册页面上的此代码之后
db.Execute(SQLINSERT, fname, lname);
添加这两行代码:
var customer_info_user_id = db.GetLastInsertId();
Response.Redirect("~/fit.cshtml?customer_info_user_id=" + customer_info_user_id);
然后将以下内容插入后续页面:
在var打开数据库之前
var customer_info_user_id = Request["customer_info_user_id"];
将已经转移到 INSERT 函数中的 id 号添加。例如:
var SQLINSERT = "INSERT INTO fit (customer_info_user_id, clothingsize) VALUES (@0,@1)";
db.Execute(SQLINSERT, customer_info_user_id, clothingsize);
将身份证号码结转到下一页
Response.Redirect("~/flatter.cshtml?customer_info_user_id=" + customer_info_user_id);
感谢@Mike Brind 和 ASP.net 论坛帮助我解决这个问题 :-)