OleDbConnection OleDbConnection OleDbConnection OleDbConnection Class


表示資料來源的開啟連接。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已開啟並設定為ConnectionOleDbCommandThe OleDbConnection is opened and set as the Connection for the OleDbCommand. 此範例接著會呼叫ExecuteNonQuery並關閉連線。The example then calls ExecuteNonQuery and closes the connection. 若要這麼做,ExecuteNonQuery傳遞連接字串和查詢字串,是 SQL INSERT 陳述式。To 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.
        catch (Exception ex)
        // 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.
        Catch ex As Exception
        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.

您可以開啟多個 DataReader 的單一OleDbConnectionYou 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. 因此,您必須明確地關閉連接呼叫Close或是處置,或使用OleDbConnection物件內Using陳述式。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. 當您使用.NET Framework Data Provider for OLE DB 時,您不必啟用連接共用,因為提供者會自動管理。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. 如需有關如何使用連線共用與.NET Framework Data Provider for OLE DB 中,請參閱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() OleDbConnection() OleDbConnection()

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

OleDbConnection(String) OleDbConnection(String) OleDbConnection(String) OleDbConnection(String)

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


CanRaiseEvents CanRaiseEvents CanRaiseEvents CanRaiseEvents

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

(Inherited from Component)
ConnectionString ConnectionString ConnectionString ConnectionString

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

ConnectionTimeout ConnectionTimeout ConnectionTimeout ConnectionTimeout

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

Container Container Container Container

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

(Inherited from Component)
Database Database Database Database

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

DataSource DataSource DataSource DataSource

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

DesignMode DesignMode DesignMode DesignMode

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

(Inherited from Component)
Events Events Events Events

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

(Inherited from Component)
Provider Provider Provider Provider

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

ServerVersion ServerVersion ServerVersion ServerVersion

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

Site Site Site Site

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

(Inherited from Component)
State State State State

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


BeginTransaction() BeginTransaction() BeginTransaction() BeginTransaction()

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

BeginTransaction(IsolationLevel) BeginTransaction(IsolationLevel) BeginTransaction(IsolationLevel) BeginTransaction(IsolationLevel)

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

ChangeDatabase(String) ChangeDatabase(String) ChangeDatabase(String) ChangeDatabase(String)

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

Close() Close() Close() Close()

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

CreateCommand() CreateCommand() CreateCommand() CreateCommand()

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

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

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

(Inherited from MarshalByRefObject)
Dispose() Dispose() Dispose() Dispose()

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

(Inherited from Component)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

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

(Inherited from Component)
EnlistDistributedTransaction(ITransaction) EnlistDistributedTransaction(ITransaction) EnlistDistributedTransaction(ITransaction) EnlistDistributedTransaction(ITransaction)

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

EnlistTransaction(Transaction) EnlistTransaction(Transaction) EnlistTransaction(Transaction) EnlistTransaction(Transaction)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

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

(Inherited from MarshalByRefObject)
GetOleDbSchemaTable(Guid, Object[]) GetOleDbSchemaTable(Guid, Object[]) GetOleDbSchemaTable(Guid, Object[]) 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() GetSchema() GetSchema() GetSchema()

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

GetSchema(String) GetSchema(String) GetSchema(String) GetSchema(String)

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

GetSchema(String, String[]) GetSchema(String, String[]) GetSchema(String, String[]) 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) GetService(Type) GetService(Type) GetService(Type)

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

(Inherited from Component)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

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

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

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

(Inherited from MarshalByRefObject)
Open() Open() Open() Open()

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

ReleaseObjectPool() ReleaseObjectPool() ReleaseObjectPool() ReleaseObjectPool()

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

ResetState() ResetState() ResetState() ResetState()

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

ToString() ToString() ToString() ToString()

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

(Inherited from Component)


Disposed Disposed Disposed Disposed

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

(Inherited from Component)
InfoMessage InfoMessage InfoMessage InfoMessage

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

StateChange StateChange StateChange StateChange


ICloneable.Clone() ICloneable.Clone() ICloneable.Clone() ICloneable.Clone()

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

IDbConnection.BeginTransaction() IDbConnection.BeginTransaction() IDbConnection.BeginTransaction() IDbConnection.BeginTransaction()

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

IDbConnection.BeginTransaction(IsolationLevel) IDbConnection.BeginTransaction(IsolationLevel) IDbConnection.BeginTransaction(IsolationLevel) IDbConnection.BeginTransaction(IsolationLevel)

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

IDbConnection.CreateCommand() IDbConnection.CreateCommand() IDbConnection.CreateCommand() IDbConnection.CreateCommand()

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