我用Adapter 和 Fill()  这种方式想dataset添加了一个表 起名为Table_Score
表中有一列记录的是分数
我想用 MAX() 求出这一列分数的最大值 然后记录下这个最大值 后边有用。string sqlstr="SELECT MAX(Score) AS 最大分值 FROM Table_Score WHERE ItemID='02010102'";
Sql语句不知道写的对不对 接下来又该怎样写呢?
我尝试着如下写了写SqlCommand cmd = new SqlCommand(sqlstr, conn);
SqlDataReader reader = cmd.ExecuteReader();
然后用reader读取最大值  调了半天不对请大侠指点下 或者给个思路吧 感激涕零!!!

解决方案 »

  1.   

     public static DataSet GetDataSet(string sql)   
            {   
                SqlDataAdapter sda =new SqlDataAdapter(sql,ConnectionString);   
                DataSet ds=new DataSet();   
                sda.Fill(ds);   
                return ds;   
            } 
    从返回的dataset里取值 
      

  2.   

    木有问题啊,关键看你后面的代码是怎么写的,reader读数据的那段
      

  3.   

    你添加的表添到数据库里了么,如果只是对dataset操作用不到sql
      

  4.   

    string sql="SELECT MAX(Score) AS 最大分值 FROM Table_Score WHERE ItemID='02010102'";
    SqlDataAdapter myDA =new SqlDataAdapter(sql,ConnectionString);  
    DataSet myDS=new DataSet();  
    myDA.Fill(myDS);  
    然后再去象下面这样读取DateSet里面的值
    myDS.Tables[0].Row[0][0].ToString();
      

  5.   

    SQL语句从数据库中读取,然后将数据填充到dataset中的Table,再取Table中的对应字段值。
      

  6.   

    C# Codestring comdstr="SELECT MAX(Score) AS 最大分值 FROM Table_Score WHERE ItemID='02010102'"; 
     SqlConnection conn=new SqlConnection ();
                conn.ConnectionString=connstr ;
                SqlCommand comd=new SqlCommand ();
                comd.CommandType =CommandType .Text ;
                comd.CommandText =comdstr ;
                comd.Connection =conn ;
                SqlDataReader myread;
                try
                {
                    conn.Open();
                    myread = comd.ExecuteReader();
                    if (myread.HasRows)
                    {
                       myread.read();
    return myread[0].Tostring();
     
                    }         
                    myread.Close();
                    myread = null;
                    conn.Close();
                }
                catch (SqlException ex) 
                { 
                    MessageBox.Show(ex.Message, "SqlException", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                { 
                    if (conn.State == ConnectionState.Open)conn.Close(); 
                }
      

  7.   

    谢谢各位的回答
    我最终使用comput的方法解决了object maxObject = ds.Tables["2"].Compute("MAX(Score)", "ItemID = '" + dr1["ItemID"] + "'");
                                    float maxscore = float.Parse(maxObject.ToString());//获取到了该类项目的最大分值