語言

OdbcConnection.Close 方法

定義

關閉與數據源的連接。

public:
 override void Close();
public:
 virtual void Close();
public override void Close();
public void Close();
override this.Close : unit -> unit
abstract member Close : unit -> unit
override this.Close : unit -> unit
Public Overrides Sub Close ()
Public Sub Close ()

實作

範例

以下範例建立 , OdbcConnection開啟它,顯示部分屬性,然後關閉連接。

private static void CreateOdbcConnection(string connectionString)
{
    using (OdbcConnection connection = new OdbcConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: " + connection.ServerVersion
            + "\nDatabase: " + connection.Database);

        // The connection is automatically closed at
        // the end of the Using block.
    }
}
Private Sub CreateOdbcConnection(ByVal connectionString As String)
    Using connection As New OdbcConnection(connectionString)
        With connection
            .Open()
            Console.WriteLine("ServerVersion: " & .ServerVersion _
               & vbCrLf + "Database: " & .Database)
        End With

        ' The connection is automatically closed
        ' at the end of the Using block.
    End Using
End Sub

備註

此 Close 方法會回滾所有待處理的交易。 接著會釋放連線至連線池,或在關閉連線池時關閉連線。 若 Close 在處理 StateChange 事件時被呼叫,則不會觸發其他 StateChange 事件。

應用程式可以呼叫 Close 超過一次,而不會產生例外。

Note

當你使用 .NET Framework Data Provider for ODBC 時,不需要啟用連線池,因為 ODBC 驅動程式管理器會自動管理這件事。 欲了解更多如何啟用與停用連線池的資訊,請參閱 Microsoft 開放資料庫連接(ODBC)文件。

注意事項

不要在你的類別方法中呼叫 Close 或 Dispose 在 Connection、DataReader 或其他受管理物件 Finalize 上。 在終結者中,你應該只釋放你職業直接擁有的未管理資源。 如果你的類別沒有擁有任何非管理資源,請不要在類別定義中包含某個 Finalize 方法。 如需詳細資訊,請參閱垃圾回收(Garbage Collection, GC)。

適用於

另請參閱