SqlCommand mycom = new SqlCommand("select count(*) as id  from Table1", mycon);
            mycon.Open();
            mycom.ExecuteNonQuery();
            mycon.Close();我该怎样获得  id的值呢?如果我SqlCommand mycom = new SqlCommand("select count(*) as id  from Table1", mycon);
            mycon.Open();
            int n = mycom.ExecuteNonQuery();
            mycon.Close();
n的值是不是所有的行数呢》?我试了下不行。。如果用第一种情况该如何写 啊??

解决方案 »

  1.   

    http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.executescalar.aspx
      

  2.   

     int n =convert.toint32( agCMD.ExecuteScalar());用这个
      

  3.   

    int n = (int)mycom.ExecuteScalar();
      

  4.   

    写一个方法:
    public static string CountA()
    {
    SqlConnection con=new SqlConnection("server=.;database=数据库名;uid=sa");
    con.Open();
    string strcmd="select count(*)from 表名 where ACardID is not null;
    SqlCommand command=newSqlCommand(strcmd,con);
    string A=command.Scalar().ToString();
    con.Close();
    return A;
    }
      

  5.   

    int n =Convert.ToInt32(mycom.ExecuteScalar());
      

  6.   

    select count(*) 肯定返回一个数字,受影响的条数就是一条
    ExecuteNonQuery()当然就是1。
    要想得到count(*)的结果ExecuteScalar(),返回第一行第一列的值。