自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET獲得當(dāng)前插入行主鍵的代碼

開發(fā) 后端
本文介紹了在asp.net中向數(shù)據(jù)庫中插入數(shù)據(jù)時(shí)如何獲得當(dāng)前插入行的主鍵。

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

  1. // asp.net獲得當(dāng)前插入行主鍵
  2. //在數(shù)據(jù)表里創(chuàng)建一個(gè)新行,并把當(dāng)前屬性的值插入對(duì)應(yīng)的列中  
  3. public int Create()  
  4. {  
  5.   //建立數(shù)據(jù)庫連接  
  6.   SqlConnection connection = new SqlConnection(_Connectionstring);  
  7.   connection.open();//打開數(shù)據(jù)庫連接  
  8.   //建立數(shù)據(jù)庫連接對(duì)象  
  9.   SqlCommand command = new SqlCommand("insert into Customers " 
  10.    +"(LastName,FirstName,Address,City,State,Zip,Phone," 
  11.    +"SignUpDate) values (@LastName,@FirstName,@Address," 
  12.    +"@City,@Zip,@Phone,@SignUpDate)",connection);  
  13.      
  14.    //將要插入的數(shù)據(jù)加入數(shù)據(jù)庫中  
  15.    command.Parameters.AddWithValue("@LastName",_LastName);  
  16.    command.Parameters.AddWithValue("@FirstName",_FirstName);  
  17.    command.Parameters.AddWithValue("@Address",_Address);  
  18.    command.Parameters.AddWithValue("@City",_City);  
  19.    command.Parameters.AddWithValue("@Zip",_Zip);  
  20.    command.Parameters.AddWithValue("@Phone",_Phone);  
  21.    command.Parameters.AddWithValue("@SingUpDate",_SingUpDate);  
  22.      
  23.    command.ExecuteNonQuery();//執(zhí)行連接語句  
  24.    command.Parameters.Clear();  
  25.    command.CommandText = "select @@IDENTITY"//查找主鍵:asp.net獲得當(dāng)前插入行主鍵  
  26.    int newCustomerID = Convert.ToInt32(command.ExecuteScalar());  
  27.    connection.Close();//關(guān)閉連接  
  28.    _CustomerID = newCustomerID;  
  29.    return newCustomerID;     
  30. }     

【編輯推薦】

  1. ASP.NET DetailsView中顯示選中產(chǎn)品的詳細(xì)信息
  2. ASP.NET 2.0數(shù)據(jù)教程:GridView選擇行
  3. ASP.NET 2.0數(shù)據(jù)教程:GridView顯示數(shù)據(jù)
  4. ASP.NET 2.0中添加GridView到頁面
  5. 新增ASP.NET頁面時(shí)的注意事項(xiàng)
責(zé)任編輯:book05 來源: cnblogs
相關(guān)推薦

2010-03-10 09:35:18

ASP.NET緩存

2009-08-05 11:00:46

獲得RowIndexASP.NET

2009-07-27 09:29:38

ASP.NET中Jav

2009-08-04 11:29:14

HTML代碼ASP.NET控件

2009-08-04 17:16:16

ASP.NET代碼優(yōu)化

2009-07-22 17:35:23

代碼隱藏文件ASP.NET

2009-07-30 14:26:30

ASP.NET常用代碼

2009-07-22 17:45:35

ASP.NET教程

2009-07-30 14:32:18

ASP.NET常用代碼

2009-08-03 14:22:33

什么是ASP.NET

2009-07-28 17:17:19

ASP.NET概述

2009-08-10 17:17:10

ASP.NET安裝部署

2009-07-27 12:22:03

ASP.NET和ASPASP.NET入門教程

2009-07-27 10:35:33

TypeConvertASP.NET

2009-07-29 11:19:03

JavaScriptASP.NET

2009-07-29 14:52:12

IScriptContASP.NET

2009-07-29 16:33:28

GreeterLogiASP.NET

2009-07-27 15:48:43

2009-07-27 10:22:16

ASP.NET中Coo

2011-05-13 15:31:24

VC++
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)