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】可以。
求解啊!
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】可以。
求解啊!
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();//错误
where IsSuccess='success'你只查询了一列 max(Time)当然是这样子啦。
string selectcommand = "select max(Time)from Electrical_Parameter_C25_X83
where IsSuccess='success'";
SqlCommand sqlCmd=new SqlCommand(selectcommand ,sc)
label2.Text = sqlCmd.ExecuteScalar().toString();
string selectcommand = "select max(Time) as Time from Electrical_Parameter_C25_X83
where IsSuccess='success'";然后使用 ds.Tables[0].Rows[0]["Time"].ToString()因为你的结果集中只有一列所以只能[0],[1]肯定是会报错的
select max(Time) as Time from ...
我确实没有空格,那也有关系?
还有,即使查询了一列,那【"Time"】为啥不行啊
使用Ado.net提供的Command对象并调用cmd.ExecuteScalar()方法,可以取得单指的查询。另外如果用你所写的方法,Fill完的DataTable中只会存在一列 max(Time),所以无论列名Time还是列下表1 都是找不到的。