【问题标题】:How do I use LAST_INSERT_ID() in WebMatrix如何在 WebMatrix 中使用 LAST_INSERT_ID()
【发布时间】:2011-12-22 22:33:09
【问题描述】:

我无法弄清楚如何在 WebMatrix 中将 LAST_INSERT_ID() 用于 MySQL 数据库。有人可以告诉我如何将它应用到下面的代码 sn-p 中吗?

var clothingsize="";
if(IsPost){     
clothingsize =Request["clothingsize"];         

var SQLINSERT = "INSERT INTO fit1 (clothingsize) VALUES (@0)"; }

【问题讨论】:

标签: .net mysql webmatrix last-insert-id


【解决方案1】:

我可以假设您使用的是 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();

编码愉快!

【讨论】:

  • 我没有使用连接器,但我认为 Mike 刚刚回答了我的问题 :-)
【解决方案2】:

这是有效的解决方案:

注册页面上此代码之后

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 论坛帮助我解决这个问题 :-)

【讨论】:

    猜你喜欢
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多