簡體   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