用ExecuteScalar执行着一个sql语句,返回的值就是ID

解决方案 »

  1.   

    int x = (int)Cmd.ExecuteScalar();
      

  2.   

    SqlConnection conn = new SqlConnection(connectionstring);
    SqlCommand comm = new SqlCommand();
    conn.open();
    comm.Connection = conn;
    comm.CommandText = "INSERT  INTO  jobs  (name)VALUES  ('aaa') ";
    comm.ExecuteScalar();
    comm.CommandText ="SELECT  @@IDENTITY  AS  'Identity'  ";
    object key = comm.ExecuteScalar();
    conn.close();
    return key;
      

  3.   

    SqlCommand comm=new SqlCommand();
    string sqlstr="insert into.....";
    int id=Int32.Parse(comm.ExecuteScalar(sqlstr));
      

  4.   

    sql = "insert into table1 (username,age) values ('zhang',20);select @@identity as 'id'";
    int i = Convert.ToInt32(Command.ExecuteScalar());