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 一次以上,而不會產生例外狀況。

注意

當您使用 .NET Framework Data Provider for ODBC 時,您不需要啟用連線共用,因為 ODBC 驅動程式管理員會自動管理這一點。 如需如何啟用和停用連線共用的詳細資訊,請參閱 Microsoft Open Database Connectivity (ODBC) 檔。

警告

請勿在 Connection、DataReader 或任何其他 Managed 物件的 方法上Finalize呼叫 CloseDispose 。 在完成項中,您應該只釋放類別直接擁有的 Unmanaged 資源。 如果類別未擁有任何 Unmanaged 資源,請不要在類別定義中包含 Finalize 方法。 如需詳細資訊,請參閱記憶體回收

適用於

另請參閱