1

我想ExecuteQuery()同时从另一个结果集中读取数据ExecuteQuery

SqlCommand cmd1 = new SqlCommand("pgwp_teamtypeselect", con);
cmd1.CommandType = CommandType.StoredProcedure;

rdr = cmd1.ExecuteReader();

string ehtml = null;
string tipi = null;
int che = 0;

while (rdr.Read())
{
    SqlCommand cmd2 = new SqlCommand("pgwp_goalselect", con);
    cmd2.CommandType = CommandType.StoredProcedure;

    rdr2 = cmd2.ExecuteReader();

    while (rdr2.Read())
    {
        do something.....
    }
}
4

1 回答 1

1

使用 in 查询执行查询没有任何问题,但我建议使用下面的示例中的使用,以便在执行完成后处理该对象

using(var command = new SqlCommand("pgwp_teamtypeselect", connection))
{
  command.CommandType = CommandType.StoredProcedure;
  using(var dataReader = command.ExecuteReader())
  {
    while (dataReader.Read()) 
    {
          using(var command1 = new SqlCommand("pgwp_goalselect",  
                       connection))
           {
             command1.CommandType = CommandType.StoredProcedure;

             using(var dataReader2 =command1.ExecuteReader())
             {
             }
           }
    }
}

}

您还应该将其添加到连接字符串MultipleActiveResultSets=True"

string connectionString = @"Data Source=GTL-263\SQLEXPRESS;Initial Catalog=master;Integrated Security=SSPI;MultipleActiveResultSets=True";

您可以阅读:Multiple Active Result Sets (MARS - ADO.NET 2.0)您正在做的事情是正确的。

于 2015-07-09T17:12:11.610 回答