How do I use LAST_INSERT_ID() in WebMatrix
-
27-03-2021 - |
سؤال
I'm having trouble figuring out how to use LAST_INSERT_ID()
for a MySQL database in WebMatrix. Can someone show me how to apply it to the code snippet below?
var clothingsize="";
if(IsPost){
clothingsize =Request["clothingsize"];
var SQLINSERT = "INSERT INTO fit1 (clothingsize) VALUES (@0)"; }
المحلول 2
Here's the solution that works:
Right after this code on the signup page
db.Execute(SQLINSERT, fname, lname);
Add these 2 lines of code:
var customer_info_user_id = db.GetLastInsertId();
Response.Redirect("~/fit.cshtml?customer_info_user_id=" + customer_info_user_id);
Then insert the following into the subsequent pages:
Before the var to open the database
var customer_info_user_id = Request["customer_info_user_id"];
Add the id number that's been carried over into the INSERT functions. For example:
var SQLINSERT = "INSERT INTO fit (customer_info_user_id, clothingsize) VALUES (@0,@1)";
db.Execute(SQLINSERT, customer_info_user_id, clothingsize);
Carry over the id number to the next page
Response.Redirect("~/flatter.cshtml?customer_info_user_id=" + customer_info_user_id);
Thanks to @Mike Brind and the ASP.net forum for helping me figure this out :-)
نصائح أخرى
Can I assume you're using the MySQL .NET Connector?
http://dev.mysql.com/downloads/connector/net
If so, here's a pretty simple example of what you're looking for:
http://forums.mysql.com/read.php?38,98672,98868#msg-98868
After executing your Insert-Command (C#, Connection is stored in 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();
Happy Coding!