不允许嵌套的 SqlCommand?
Not allow Nested SqlCommand?
我有 2 个 SqlCommand
,其中一个是嵌套的。为什么它不允许我发出第二个 SqlCommand
(我正在使用单独的 SQLCommand
)?它给出了一个错误
There is already an open DataReader associated with this Command which must be closed first.
如果我单独使用SqlConnection
,没问题。
SqlCommand cmd = new SqlCommand(qry, cn);
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
....
try
{
SqlCommand cmd2 = new SqlCommand(qry2, cn);
cmd2.ExecuteNonQuery();
}
catch (Exception e)
{
// I get this error here
// System.Data; There is already an open DataReader associated with this Command which must be closed first.
}
}
消息很明显:当 DataReader
仍然打开时,您不能同时对不同的 SqlCommand
实例使用相同的连接。 SqlDataReader
实例说明已经说了:
While the SqlDataReader is being used, the associated SqlConnection is
busy serving the SqlDataReader, and no other operations can be
performed on the SqlConnection other than closing it. This is the case
until the Close method of the SqlDataReader is called. For example,
you cannot retrieve output parameters until after you call Close.
此问题的常见解决方案是在连接字符串上使用 MultipleActiveResultSets=True
:
<add name="ConnectionName" connectionString="[connection string];MultipleActiveResultSets=True" ... />
然后,使用DataTable
代替直接迭代DataReader
:
var dt = new DataTable();
dt.Load(rd);
foreach (DataRow row in dt.Rows)
{
// other stuff
try
{
SqlCommand cmd2 = new SqlCommand(qry2, cn);
cmd2.ExecuteNonQuery();
}
catch (Exception e)
{
// throw exception
}
}
此外,您可以使用 SqlConnection.State
属性:
简单检查之前的连接是否仍然打开(即服务 DataReader
)
// close if connection still open
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
// open if connection already closed
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
上面的简单检查应该放在请求 SqlConnection
.
的代码的任何部分
我有 2 个 SqlCommand
,其中一个是嵌套的。为什么它不允许我发出第二个 SqlCommand
(我正在使用单独的 SQLCommand
)?它给出了一个错误
There is already an open DataReader associated with this Command which must be closed first.
如果我单独使用SqlConnection
,没问题。
SqlCommand cmd = new SqlCommand(qry, cn);
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
....
try
{
SqlCommand cmd2 = new SqlCommand(qry2, cn);
cmd2.ExecuteNonQuery();
}
catch (Exception e)
{
// I get this error here
// System.Data; There is already an open DataReader associated with this Command which must be closed first.
}
}
消息很明显:当 DataReader
仍然打开时,您不能同时对不同的 SqlCommand
实例使用相同的连接。 SqlDataReader
实例说明已经说了:
While the SqlDataReader is being used, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called. For example, you cannot retrieve output parameters until after you call Close.
此问题的常见解决方案是在连接字符串上使用 MultipleActiveResultSets=True
:
<add name="ConnectionName" connectionString="[connection string];MultipleActiveResultSets=True" ... />
然后,使用DataTable
代替直接迭代DataReader
:
var dt = new DataTable();
dt.Load(rd);
foreach (DataRow row in dt.Rows)
{
// other stuff
try
{
SqlCommand cmd2 = new SqlCommand(qry2, cn);
cmd2.ExecuteNonQuery();
}
catch (Exception e)
{
// throw exception
}
}
此外,您可以使用 SqlConnection.State
属性:
DataReader
)
// close if connection still open
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
// open if connection already closed
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
上面的简单检查应该放在请求 SqlConnection
.