我用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读取最大值 调了半天不对请大侠指点下 或者给个思路吧 感激涕零!!!
表中有一列记录的是分数
我想用 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读取最大值 调了半天不对请大侠指点下 或者给个思路吧 感激涕零!!!
{
SqlDataAdapter sda =new SqlDataAdapter(sql,ConnectionString);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
从返回的dataset里取值
SqlDataAdapter myDA =new SqlDataAdapter(sql,ConnectionString);
DataSet myDS=new DataSet();
myDA.Fill(myDS);
然后再去象下面这样读取DateSet里面的值
myDS.Tables[0].Row[0][0].ToString();
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();
}
我最终使用comput的方法解决了object maxObject = ds.Tables["2"].Compute("MAX(Score)", "ItemID = '" + dr1["ItemID"] + "'");
float maxscore = float.Parse(maxObject.ToString());//获取到了该类项目的最大分值