2011-12-22 55 views
0

我無法弄清楚如何在WebMatrix中將LAST_INSERT_ID()用於MySQL數據庫。有人能告訴我如何將它應用到下面的代碼片段嗎?如何在WebMatrix中使用LAST_INSERT_ID()

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

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

HTTP ://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html – PachinSV 2011-12-22 22:50:10

+0

典型的SO - 一些白癡投票你的問題,因爲他們不明白。請參閱此處:http://forums.asp.net/t/1752458.aspx/1?How+do+I+use+LAST_INSERT_ID+in+Webmatrix+to+connect+the+tables – 2011-12-23 05:57:26

回答

0

這裏有一個工作的解決方案:

權的註冊頁面上這個代碼後

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函數中。例如:

var SQLINSERT = "INSERT INTO fit (customer_info_user_id, clothingsize) VALUES (@0,@1)";     
db.Execute(SQLINSERT, customer_info_user_id, clothingsize); 

結轉的ID號到下一頁

Response.Redirect("~/flatter.cshtml?customer_info_user_id=" + customer_info_user_id); 

感謝@Mike Brind和ASP.net論壇幫助我想出解決辦法:-)

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(); 

快樂編碼!

+0

我沒有使用連接器,但我認爲邁克只是回答了我的問題:-) – 2011-12-23 06:38:19