//ExecuteScalar()获得自增字段的值
using (SqlConnection conn = new SqlConnection(@"Data Source=192.168.1.130; Initial Catalog=test;User ID=sa;Password=123456"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into T_Users(Username,password) output inserted.id values('test','test')";
//cmd.ExecuteScalar()用于只有一行一列的返回值获取。
long i=(long)cmd.ExecuteScalar();
MessageBox.Show(i.ToString());
}
}
评论回复