溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ASP.NET如何獲得當(dāng)前插入行主鍵

發(fā)布時(shí)間:2021-07-15 11:37:25 來(lái)源:億速云 閱讀:93 作者:chen 欄目:編程語(yǔ)言

這篇文章主要講解了“ASP.NET如何獲得當(dāng)前插入行主鍵”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“ASP.NET如何獲得當(dāng)前插入行主鍵”吧!

我們?cè)谶M(jìn)行數(shù)據(jù)庫(kù)插入或更新操作的時(shí)候,有時(shí)我們需要知道當(dāng)前插入行的數(shù)據(jù)庫(kù)表的主鍵值。那么如何實(shí)現(xiàn)asp.net獲得當(dāng)前插入行的主鍵呢?下面的代碼將實(shí)現(xiàn)獲得插入數(shù)據(jù)時(shí)的主鍵值:

// asp.net獲得當(dāng)前插入行主鍵//在數(shù)據(jù)表里創(chuàng)建一個(gè)新行,并把當(dāng)前屬性的值插入對(duì)應(yīng)的列中  public int Create()  {    //建立數(shù)據(jù)庫(kù)連接    SqlConnection connection = new SqlConnection(_Connectionstring);    connection.open();//打開數(shù)據(jù)庫(kù)連接    //建立數(shù)據(jù)庫(kù)連接對(duì)象    SqlCommand command = new SqlCommand("insert into Customers "    +"(LastName,FirstName,Address,City,State,Zip,Phone,"    +"SignUpDate) values (@LastName,@FirstName,@Address,"    +"@City,@Zip,@Phone,@SignUpDate)",connection);          //將要插入的數(shù)據(jù)加入數(shù)據(jù)庫(kù)中     command.Parameters.AddWithValue("@LastName",_LastName);     command.Parameters.AddWithValue("@FirstName",_FirstName);     command.Parameters.AddWithValue("@Address",_Address);     command.Parameters.AddWithValue("@City",_City);     command.Parameters.AddWithValue("@Zip",_Zip);     command.Parameters.AddWithValue("@Phone",_Phone);     command.Parameters.AddWithValue("@SingUpDate",_SingUpDate);          command.ExecuteNonQuery();//執(zhí)行連接語(yǔ)句     command.Parameters.Clear();     command.CommandText = "select @@IDENTITY"; //查找主鍵:asp.net獲得當(dāng)前插入行主鍵     int newCustomerID = Convert.ToInt32(command.ExecuteScalar());     connection.Close();//關(guān)閉連接     _CustomerID = newCustomerID;     return newCustomerID;     }

感謝各位的閱讀,以上就是“ASP.NET如何獲得當(dāng)前插入行主鍵”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)ASP.NET如何獲得當(dāng)前插入行主鍵這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI