繁体   English   中英

C#odbcconnection没有关闭

[英]c# odbcconnection not closing

我在C#中使用OdbcConnection遇到了问题。 我编写了一个类,将我的连接和命令包装到一个通用接口中。 代码基本上像这样(我省略了一些不重要的部分)

//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;
}

我打开一个连接,执行所需的命令,然后调用disposeConnection 但是我们的数据库(SAP Sybase SQL版本11、16、17)仍显示连接处于“ PREFETCH”状态...

执行SQL命令后,将在finally块内调用disposeConnection

为什么连接未正确关闭?

我终于找到了解决方案。 我没有打开DbDataReader 我必须将所有DbDataReaders包装在using块内。 现在,当我调用_connection.close()时,我的连接已关闭。

因此,连接是否处于关闭状态并不重要,如果有任何其他对象正在访问数据库,则连接不会真正关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM