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 ,而不会生成异常。

注意

使用适用于 ODBC 的 .NET Framework 数据提供程序时,无需启用连接池,因为 ODBC 驱动程序管理器会自动管理此连接池。 有关如何启用和禁用连接池的详细信息,请参阅 Microsoft Open Database Connectivity (ODBC) 文档。

注意

不要在 类的 方法中Finalize调用 CloseDispose 对 Connection、DataReader 或任何其他托管对象调用 或 。 在终结器中,应仅释放类直接拥有的非托管资源。 如果类不拥有任何非托管资源,则不要在类定义中包含 Finalize 方法。 有关详细信息,请参阅垃圾回收

适用于

另请参阅