Sql Server添加一条记录后获取自增的ID

 

string sql = "INSERT INTO Huawei(Name) VALUES('Chen'); SELECT @@IDENTITY as ID"; SqlConnection sc = new SqlConnection(); sc.ConnectionString = "data source=localhost; uid=sa; pwd=1985813; initial catalog=student"; sc.Open(); SqlCommand sqlcom = new SqlCommand(sql, sc); object obj = sqlcom.ExecuteScalar(); Label1.Text = Convert.ToInt32(obj) + ""; 数据库中student中有一张表Test,Test中有两个字段ID,Name.ID为自增的。以上代码可以添加一条记录,获取自增的ID。

 

你可能感兴趣的:(Sql Server添加一条记录后获取自增的ID)