我想从datareader接收两个值。但我不知道如何将其放入一个查询中。如何只使用一个sqldatareader来接收它?
string query = "select date from ars.kmstand where ritnr=(select max(ritnr) from ars.kmstand)";
string query2 = "select nr from ars.kmstand where ritnr=(select max(ritnr) from ars.kmstand)";
SqlCommand com = new SqlCommand(query2, conn);
SqlDataReader rt = com.ExecuteReader();
while (rt.Read())
{
ritnr = rt.GetInt32(0);
}
rt.Close();
SqlCommand comm = new SqlCommand(query, conn);
SqlDataReader rd = comm.ExecuteReader();
while (rd.Read())
{
value = rd.GetSqlDateTime(0);
}
答案 0 :(得分:2)
组合查询并将列索引传递给IDataReader的GetXXX方法,从结果集中获取每列的值。
string query = "select nhr, date from ars.kmstand where ritnr=(select max(ritnr) from ars.kmstand)";
SqlCommand com = new SqlCommand(query, conn);
SqlDataReader rt = com.ExecuteReader();
while (rt.Read()) {
int nhr = rt.GetInt32(0);
SqlDateTime date = rt.GetSqlDateTime(1);
}
rt.Close();
答案 1 :(得分:0)
DbDataReader
上的NextResult
方法及其派生类型(包括SqldataReader
)将从一个数据集移动到下一个数据集。
因此:
// ...
var rdr = cmd.ExecuteReader();
while (rdr.Read()) {
// Read rows from the first dataset
}
if (rdr.NextResult()) {
// There is a second dataset
while (rdr.Read()) {
// ...
}
}
在命令中,只有多个操作,每个操作都返回行(这可能在您执行的存储过程中)。