我写有一张进货表,然后我打算统计一共进了多少种货物,就写了以下语句:
       
this.myCommand.CommandText = "select distinct count(*) number from Buy";
SqlDataReader myDataReader = this.myCommand.ExecuteReader();
String num = myDataReader["number"].ToString();
MessageBox.Show("共有货物品种" + num + "种");
myDataReader.Close();
我原本打算用SQL语句:select distinct count(*) number from Buy 来做统计,然后以String num = myDataReader["number"].ToString()来取得统计数据,但是发现行不通。那么如果以select distinct count(*) number from Buy 来做统计后,如何取得结果?

解决方案 »

  1.   

    int count=(int)command.ExecuteScalar();//执行sql语句
      

  2.   

    你得先调用myDataReader.Read()方法
      

  3.   

    另外你的SQL语句写得有问题,按照你的意思应该是想找出不重复的个数,所以distinct应该在count函数内,这样:select count(distinct *) number from Buy
      

  4.   


    我看书上写的是:select distinct count(*) number from Buy
    书是人民邮电出版社出的SQL语言与数据库管理。
      

  5.   


    distinct count(*)是无意义的,因为count出来本来就只有一个数值,应该用count(distinct *)吧
      

  6.   

    int i = 0;Dim i as Integer
    i = 0
      

  7.   

    int count=(int)command.ExecuteScalar();