[英]c# odbcconnection not closing
I've got a problem with the OdbcConnection in c#. 我在C#中使用OdbcConnection遇到了问题。 I've written a class that wraps my connections and commands into a generic interface.
我编写了一个类,将我的连接和命令包装到一个通用接口中。 The code looks basically like this (I've left out some unimportant parts)
代码基本上像这样(我省略了一些不重要的部分)
//connection, command, _connectionType and _commandType are class members
private void getConnection(ref String commandText, out DbConnection connection, out DbCommand command, params DbParameter[] parameter)
{
connection = Activator.CreateInstance(this._connectionType) as DbConnection;
connection.ConnectionString = this._connectionString;
connection.Open();
command = Activator.CreateInstance(this._commandType) as DbCommand;
command.Connection = connection;
command.CommandText = commandText;
}
//Other methods use the DbCommand for SELECTS, UPDATES, etc...
private void disposeConnection()
{
this._command.Dispose();
this._connection.Close();
this._connection.Dispose();
this._command = null;
this._connection = null;
}
I open a connection, execute my desired command and the call disposeConnection
. 我打开一个连接,执行所需的命令,然后调用
disposeConnection
。 But our Database (SAP Sybase SQL ver.11,16,17) still shows the connection in the "PREFETCH" state... 但是我们的数据库(SAP Sybase SQL版本11、16、17)仍显示连接处于“ PREFETCH”状态...
The disposeConnection
is called inside a finally
block after the SQL command is executed. 执行SQL命令后,将在
finally
块内调用disposeConnection
。
Why is the connection not closed correctly? 为什么连接未正确关闭?
I've finally found the solution. 我终于找到了解决方案。 I've left the
DbDataReader
open. 我没有打开
DbDataReader
。 I had to wrap all the DbDataReaders
inside a using block. 我必须将所有
DbDataReaders
包装在using块内。 Now my connections are closed when I call _connection.close()
. 现在,当我调用
_connection.close()
时,我的连接已关闭。
So it doesn't matter if the connection is in the closed state, if any other object is acessing the database, the connection is not realy closed. 因此,连接是否处于关闭状态并不重要,如果有任何其他对象正在访问数据库,则连接不会真正关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.