OleDbConnection 類別

定義

表示資料來源的開啟連接。Represents an open connection to a data source.

public ref class OleDbConnection sealed : System::Data::Common::DbConnection, ICloneable, IDisposable
public sealed class OleDbConnection : System.Data.Common.DbConnection, ICloneable, IDisposable
type OleDbConnection = class
    inherit DbConnection
    interface ICloneable
    interface IDbConnection
    interface IDisposable
Public NotInheritable Class OleDbConnection
Inherits DbConnection
Implements ICloneable, IDisposable
繼承
實作

範例

下列範例會建立 OleDbCommandOleDbConnectionThe following example creates an OleDbCommand and an OleDbConnection. 隨即開啟 OleDbConnection,並將其設定為 OleDbCommandConnectionThe OleDbConnection is opened and set as the Connection for the OleDbCommand. 然後,此範例會呼叫 ExecuteNonQuery 並關閉連接。The example then calls ExecuteNonQuery and closes the connection. 為達成此目的,會傳遞一個連接字串和一個 SQL INSERT 語句的查詢字串給 ExecuteNonQueryTo accomplish this, ExecuteNonQuery is passed a connection string and a query string that is an SQL INSERT statement.

public void InsertRow(string connectionString, string insertSQL)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        // The insertSQL string contains a SQL statement that
        // inserts a new row in the source table.
        OleDbCommand command = new OleDbCommand(insertSQL);

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the insert command.
        try
        {
            connection.Open();
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
}
Public Sub InsertRow(ByVal connectionString As String, _
    ByVal insertSQL As String)

    Using connection As New OleDbConnection(connectionString)
        ' The insertSQL string contains a SQL statement that
        ' inserts a new row in the source table.
        Dim command As New OleDbCommand(insertSQL)

        ' Set the Connection to the new OleDbConnection.
        command.Connection = connection

        ' Open the connection and execute the insert command.
        Try
            connection.Open()
            command.ExecuteNonQuery()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        ' The connection is automatically closed when the
        ' code exits the Using block.
    End Using
End Sub

備註

OleDbConnection 物件代表資料來源的唯一連接。An OleDbConnection object represents a unique connection to a data source. 在用戶端/伺服器資料庫系統中,它相當於與伺服器的網路連接。With a client/server database system, it is equivalent to a network connection to the server. 根據原生 OLE DB 提供者所支援的功能而定,可能無法使用 OleDbConnection 物件的部分方法或屬性。Depending on the functionality supported by the native OLE DB provider, some methods or properties of an OleDbConnection object may not be available.

當您建立 OleDbConnection的實例時,所有屬性都會設為其初始值。When you create an instance of OleDbConnection, all properties are set to their initial values. 如需這些值的清單,請參閱 OleDbConnection 的函式。For a list of these values, see the OleDbConnection constructor.

您可以在單一 OleDbConnection上開啟一個以上的 DataReader。You can open more than one DataReader on a single OleDbConnection. 如果您使用的 OLE DB 提供者在單一連接上不支援一個以上的 DataReader,則提供者會隱含地為每個連線開啟其他連接。If the OLE DB provider you use does not support more than one DataReader on a single connection, the provider implicitly opens an additional connection for each.

如果 OleDbConnection 超出範圍,則不會關閉。If the OleDbConnection goes out of scope, it is not closed. 因此,您必須藉由呼叫 CloseDispose,或在 Using 語句中使用 OleDbConnection 物件,明確地關閉連接。Therefore, you must explicitly close the connection by calling Close or Dispose, or by using the OleDbConnection object within a Using statement.

注意

若要部署高效能應用程式,您必須使用連接共用。To deploy high-performance applications, you must use connection pooling. 當您使用 OLE DB 的 .NET Framework Data Provider 時,您不需要啟用連接共用,因為提供者會自動管理此服務。When you use the .NET Framework Data Provider for OLE DB, you do not have to enable connection pooling because the provider manages this automatically. 如需有關如何將連接共用與 OLE DB 的 .NET Framework Data Provider 搭配使用的詳細資訊,請參閱OLE DB、ODBC 和 Oracle 連接共用。For more information about how to use connection pooling with the .NET Framework Data Provider for OLE DB, see OLE DB, ODBC, and Oracle Connection Pooling.

如果嚴重的 OleDbException (例如,SQL Server 嚴重性層級20或更大)是由執行 OleDbCommand的方法所產生,則 OleDbConnection 可能會關閉。If a fatal OleDbException (for example, a SQL Server severity level of 20 or greater) is generated by the method executing an OleDbCommand, the OleDbConnection might be closed. 但是,使用者可以再次開啟連線,然後繼續進行。However, the user can reopen the connection and continue.

建立 OleDbConnection 物件實例的應用程式,可以透過設定宣告式或命令式安全性要求,要求所有直接和間接呼叫端都具有足夠的程式碼許可權。An application that creates an instance of the OleDbConnection object can require all direct and indirect callers to have sufficient permission to the code by setting declarative or imperative security demands. OleDbConnection 使用 OleDbPermission 物件來進行安全性要求。OleDbConnection makes security demands using the OleDbPermission object. 使用者可以使用 OleDbPermissionAttribute 物件來驗證其程式碼是否有足夠的許可權。Users can verify that their code has sufficient permissions by using the OleDbPermissionAttribute object. 使用者和系統管理員也可以使用caspol.exe (代碼啟用安全性原則工具)來修改電腦、使用者和企業層級的安全性原則。Users and administrators can also use the Caspol.exe (Code Access Security Policy Tool) to modify security policy at the computer, user, and enterprise levels. 如需詳細資訊,請參閱 Code Access Security and ADO.NETFor more information, see Code Access Security and ADO.NET.

如需處理來自資料伺服器之警告和參考用訊息的詳細資訊,請參閱連接事件For more information about handling warning and informational messages from the data server, see Connection Events.

注意

OleDbConnection 物件不支援設定或抓取 OLE DB 提供者特定的動態屬性。The OleDbConnection object does not support setting or retrieving dynamic properties specific to an OLE DB provider. 僅支援 OLE DB 提供者之連接字串中可傳遞的屬性。Only properties that can be passed in the connection string for the OLE DB provider are supported.

建構函式

OleDbConnection()

初始化 OleDbConnection 類別的新執行個體。Initializes a new instance of the OleDbConnection class.

OleDbConnection(String)

使用指定的連接字串,初始化 OleDbConnection 類別的新執行個體。Initializes a new instance of the OleDbConnection class with the specified connection string.

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。Gets a value indicating whether the component can raise an event.

(繼承來源 Component)
ConnectionString

取得或設定用來開啟資料庫的字串。Gets or sets the string used to open a database.

ConnectionTimeout

取得在終止嘗試並產生錯誤前嘗試建立連接的等待時間 (以秒為單位)。Gets the time to wait (in seconds) while trying to establish a connection before terminating the attempt and generating an error.

Container

取得包含 IContainerComponentGets the IContainer that contains the Component.

(繼承來源 Component)
Database

取得目前資料庫或要在連接開啟之後使用的資料庫名稱。Gets the name of the current database or the database to be used after a connection is opened.

DataSource

取得資料來源的伺服器名稱或檔案名稱。Gets the server name or file name of the data source.

DesignMode

取得值,指出 Component 目前是否處於設計模式。Gets a value that indicates whether the Component is currently in design mode.

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。Gets the list of event handlers that are attached to this Component.

(繼承來源 Component)
Provider

取得在連接字串的 "Provider= " 子句中指定的 OLE DB 提供者名稱。Gets the name of the OLE DB provider specified in the "Provider= " clause of the connection string.

ServerVersion

取得字串,其包含用戶端連接之伺服器的版本。Gets a string that contains the version of the server to which the client is connected.

Site

取得或設定 ComponentISiteGets or sets the ISite of the Component.

(繼承來源 Component)
State

取得連線目前的狀態。Gets the current state of the connection.

方法

BeginTransaction()

使用目前的 IsolationLevel 值來開始資料庫的交易。Starts a database transaction with the current IsolationLevel value.

BeginTransaction(IsolationLevel)

使用指定的隔離等級開始資料庫異動。Starts a database transaction with the specified isolation level.

ChangeDatabase(String)

為開啟的 OleDbConnection 變更目前的資料庫。Changes the current database for an open OleDbConnection.

Close()

關閉資料來源的連接。Closes the connection to the data source.

CreateCommand()

建立並傳回與 OleDbCommand 相關聯的 OleDbConnection 物件。Creates and returns an OleDbCommand object associated with the OleDbConnection.

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。Releases all resources used by the Component.

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(繼承來源 Component)
EnlistDistributedTransaction(ITransaction)

將指定的異動登記為分散式異動。Enlists in the specified transaction as a distributed transaction.

EnlistTransaction(Transaction)

將指定的異動登記為分散式異動。Enlists in the specified transaction as a distributed transaction.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetOleDbSchemaTable(Guid, Object[])

在套用指定的限制之後,從資料來源傳回 GUID 所指示的結構描述 (Schema) 資訊。Returns schema information from a data source as indicated by a GUID, and after it applies the specified restrictions.

GetSchema()

傳回這個 OleDbConnection 之資料來源的結構描述資訊。Returns schema information for the data source of this OleDbConnection.

GetSchema(String)

使用結構描述名稱的特定字串,傳回這個 OleDbConnection 之資料來源的結構描述資訊。Returns schema information for the data source of this OleDbConnection using the specified string for the schema name.

GetSchema(String, String[])

使用結構描述名稱的特定字串和限制值的特定字串陣列,傳回這個 OleDbConnection 的資料來源的結構描述資訊。Returns schema information for the data source of this OleDbConnection using the specified string for the schema name and the specified string array for the restriction values.

GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。Returns an object that represents a service provided by the Component or by its Container.

(繼承來源 Component)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
Open()

使用 ConnectionString 所指定的屬性設定,開啟資料庫連接。Opens a database connection with the property settings specified by the ConnectionString.

ReleaseObjectPool()

指出 OleDbConnection 物件集區 (Object Pool) 可在釋放最後一個基礎連接時釋放。Indicates that the OleDbConnection object pool can be released when the last underlying connection is released.

ResetState()

更新 State 物件的 OleDbConnection 屬性。Updates the State property of the OleDbConnection object.

ToString()

傳回任何包含 Component 名稱的 StringReturns a String containing the name of the Component, if any. 不應覆寫此方法。This method should not be overridden.

(繼承來源 Component)

事件

Disposed

當此元件由 Dispose() 方法的呼叫處置時發生。Occurs when the component is disposed by a call to the Dispose() method.

(繼承來源 Component)
InfoMessage

當提供者傳送警告或資訊訊息時發生。Occurs when the provider sends a warning or an informational message.

StateChange

明確介面實作

ICloneable.Clone()

如需這個成員的說明,請參閱 Clone()For a description of this member, see Clone().

IDbConnection.BeginTransaction()

開始資料庫異動。Begins a database transaction.

IDbConnection.BeginTransaction(IsolationLevel)

使用指定的隔離等級開始資料庫異動。Begins a database transaction with the specified isolation level.

IDbConnection.CreateCommand()

建立並傳回與連線相關聯的命令物件。Creates and returns a command object associated with the connection.

適用於

另請參閱