SqlConnection sc = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
            string selectcommand = "select max(Time)from Electrical_Parameter_C25_X83 
                                    where IsSuccess='success'";
            SqlDataAdapter sda = new SqlDataAdapter(selectcommand, sc);
            DataSet ds = new DataSet();
            sda.Fill(ds, "temp");
            label2.Text = ds.Tables[0].Rows[0]["Time"].ToString();//错误
表:Electrical_Parameter_C25_X83 
其中两列:Time(DateTime,主键),IsSuccess(nchar)
我想获得 最大时间的数据报错:列“Time”不属于表 temp。
Time是表中第一个列,把【"Time"】换成【0】就可以了,但是换成【1】就错了。(表中有9列,“1”不超范围)
无论是用string还是index,只有【0】可以。
求解啊!

解决方案 »

  1.   

    SqlConnection sc = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
      string selectcommand = "select max(Time) as Time from Electrical_Parameter_C25_X83  
      where IsSuccess='success'";
      SqlDataAdapter sda = new SqlDataAdapter(selectcommand, sc);
      DataSet ds = new DataSet();
      sda.Fill(ds, "temp");
      label2.Text = ds.Tables[0].Rows[0]["Time"].ToString();//错误
      

  2.   

    "select max(Time)from Electrical_Parameter_C25_X83 
      where IsSuccess='success'你只查询了一列 max(Time)当然是这样子啦。
      

  3.   

    你不要那么写!SqlConnection sc = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
      string selectcommand = "select max(Time)from Electrical_Parameter_C25_X83 
      where IsSuccess='success'";
      SqlCommand sqlCmd=new SqlCommand(selectcommand ,sc)
      label2.Text = sqlCmd.ExecuteScalar().toString();
      

  4.   

    你可以把查询语句改为
    string selectcommand = "select max(Time) as Time from Electrical_Parameter_C25_X83 
      where IsSuccess='success'";然后使用 ds.Tables[0].Rows[0]["Time"].ToString()因为你的结果集中只有一列所以只能[0],[1]肯定是会报错的
      

  5.   

    select max(Time)from //这只返回一个值,  =〉 select max(Time) as Time from
      

  6.   

    你的selectcommand是聚合查询,没有指定列名,哪来的Time列?
    select max(Time) as Time from ...
      

  7.   

    别的不说你那Max(Time)和From之间应该有个空格吧
      

  8.   

    太感动了,谢谢大家
    我确实没有空格,那也有关系?
    还有,即使查询了一列,那【"Time"】为啥不行啊
      

  9.   

    谢谢了,加上as Time 果然对了啊。
      

  10.   

    SQL 问题,max(Time)不会默认为Time字段滴
      

  11.   

    如果你只是为了取得一个最大值的话, 完全没有必要使用DataSet,要知道Fill方法的执行开销是很大的。
    使用Ado.net提供的Command对象并调用cmd.ExecuteScalar()方法,可以取得单指的查询。另外如果用你所写的方法,Fill完的DataTable中只会存在一列 max(Time),所以无论列名Time还是列下表1 都是找不到的。