c#odbcconnection未关闭

时间:2015-11-23 12:33:46

标签: c# odbc sybase-asa

我在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 ver.11,16,17)仍然显示" PREFETCH"状态...

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

为什么连接未正确关闭?

1 个答案:

答案 0 :(得分:0)

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

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

相关问题