I am using C# + .Net 3.5 + VSTS 2008 + ADO.Net + SQL Server 2008. And I am sharing one single SQL Connection object (TestDBConnection variable in my below sample) within my application.
我正在使用C#+ .Net 3.5 + VSTS 2008 + ADO.Net + SQL Server 2008.我正在我的应用程序*享一个单独的SQL连接对象(我的下面示例中的TestDBConnection变量)。
The exception I met with is, "There is already an open DataReader associated with this Command which must be closed first.." Any ideas what is wrong?
我遇到的例外情况是,“已经有一个与此命令关联的开放DataReader必须首先关闭..”任何想法有什么问题?
The patterns within my application which I am using are all like this, i.e. sharing the single db connection object TestDBConnection, and using the single TestDBConnection variable to create command on it and execute store procedure.
我正在使用的应用程序中的模式都是这样的,即共享单个数据库连接对象TestDBConnection,并使用单个TestDBConnection变量在其上创建命令并执行存储过程。
using (SqlCommand testCommand = new SqlCommand())
{
testCommand.Connection = TestDBConnection;
testCommand.CommandType = CommandType.StoredProcedure;
testCommand.CommandText = "prc_AddOrderStatus";
testCommand.Parameters.Add("@orderID", SqlDbType.NVarChar).Value = orderID;
testCommand.ExecuteNonQuery();
}
thanks in advance, George
乔治,提前谢谢
3 个解决方案
#1
Don't share the connection, use connection pooling instead. If you are doing two things at the same time on the connection, you might want to look into MARS.
不要共享连接,而是使用连接池。如果您在连接上同时执行两项操作,则可能需要查看MARS。
For a test add this to your connection string: ;MultipleActiveResultSets=True;
and see if this "fixes" the error. A lot of people believe you should avoid using MARS, so this is something to consider.
对于测试,将此添加到您的连接字符串:; MultipleActiveResultSets = True;并查看这是否“修复”了错误。很多人认为你应该避免使用MARS,所以这是需要考虑的事情。
#2
using (sqlConnection theconnection = new sqlconnection(initialise it))
{
using (SqlCommand testCommand = new SqlCommand())
{
testCommand.Connection = theConnection
testCommand.CommandType = CommandType.StoredProcedure;
testCommand.CommandText = "prc_AddOrderStatus";
testCommand.Parameters.Add("@orderID", SqlDbType.NVarChar).Value = orderID;
testCommand.ExecuteNonQuery();
}
}
is the pattern that i use in multi threaded cases with no problems at all.
是我在多线程案例中使用的模式,完全没有问题。
Incidently this is connection pooling.
这是连接池。
#3
George, is it possible that the exception is telling you the truth? Are there any other commands that you've started but not yet finished?
乔治,这个例外有可能告诉你真相吗?是否还有其他命令已经启动但尚未完成?
#1
Don't share the connection, use connection pooling instead. If you are doing two things at the same time on the connection, you might want to look into MARS.
不要共享连接,而是使用连接池。如果您在连接上同时执行两项操作,则可能需要查看MARS。
For a test add this to your connection string: ;MultipleActiveResultSets=True;
and see if this "fixes" the error. A lot of people believe you should avoid using MARS, so this is something to consider.
对于测试,将此添加到您的连接字符串:; MultipleActiveResultSets = True;并查看这是否“修复”了错误。很多人认为你应该避免使用MARS,所以这是需要考虑的事情。
#2
using (sqlConnection theconnection = new sqlconnection(initialise it))
{
using (SqlCommand testCommand = new SqlCommand())
{
testCommand.Connection = theConnection
testCommand.CommandType = CommandType.StoredProcedure;
testCommand.CommandText = "prc_AddOrderStatus";
testCommand.Parameters.Add("@orderID", SqlDbType.NVarChar).Value = orderID;
testCommand.ExecuteNonQuery();
}
}
is the pattern that i use in multi threaded cases with no problems at all.
是我在多线程案例中使用的模式,完全没有问题。
Incidently this is connection pooling.
这是连接池。
#3
George, is it possible that the exception is telling you the truth? Are there any other commands that you've started but not yet finished?
乔治,这个例外有可能告诉你真相吗?是否还有其他命令已经启动但尚未完成?