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
继承
实现

示例

下面的示例创建OleDbCommand和一个OleDbConnectionThe 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.
        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.

您可以打开多个 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. 因此,必须显式关闭连接通过调用CloseDispose,或使用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. 当用于 OLE DB 使用.NET Framework 数据提供程序时,您无需启用连接池,因为提供程序会自动管理。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 数据提供程序用于 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

获取 IContainer,它包含 ComponentGets 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)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。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 占用的非托管资源,还可以另外再释放托管资源。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()

检索控制此实例的生存期策略的当前生存期服务对象。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 的指示从数据源返回架构信息。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 的浅表副本。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 对象池。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 的名称的 String(如果有)。Returns 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.

适用于

另请参阅