Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合查询并使用1个datareader_C#_Sql_Ado.net - Fatal编程技术网

C# 组合查询并使用1个datareader

C# 组合查询并使用1个datareader,c#,sql,ado.net,C#,Sql,Ado.net,我想从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

我想从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);
        }
DbDataReader
上的方法及其派生类型(包括
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()) {
    // ...
  }
}

在命令中,只需执行多个操作,每个操作返回行(这可能在您执行的存储过程中)。

组合查询并将列索引传递给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();   

谢谢,更短更好。真的很有帮助!