如何在 c# 中从另一个 sql 查询读取数据时执行查询?
How to executequery while reading data from another sql query in c#?
我想 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.....
}
}
使用 in query 执行查询没有任何问题,但我建议使用如下示例中的 using ,以便在执行完成后处理该对象
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)你做的事情是正确的。
我想 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.....
}
}
使用 in query 执行查询没有任何问题,但我建议使用如下示例中的 using ,以便在执行完成后处理该对象
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)你做的事情是正确的。