极品分享

C#:ExecuteScalar()获得自增字段的值


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

2013-04-07 0 /
NET学习
/
标签: 

评论回复

回到顶部