OleDbConnection 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示到数据源的连接是打开的。
public ref class OleDbConnection sealed : System::Data::Common::DbConnection, ICloneable, IDisposable
public ref class OleDbConnection sealed : System::ComponentModel::Component, ICloneable, IDisposable, System::Data::IDbConnection
public ref class OleDbConnection sealed : System::Data::Common::DbConnection, ICloneable
public sealed class OleDbConnection : System.Data.Common.DbConnection, ICloneable, IDisposable
public sealed class OleDbConnection : System.ComponentModel.Component, ICloneable, IDisposable, System.Data.IDbConnection
public sealed class OleDbConnection : System.Data.Common.DbConnection, ICloneable
type OleDbConnection = class
inherit DbConnection
interface ICloneable
interface IDbConnection
interface IDisposable
type OleDbConnection = class
inherit Component
interface ICloneable
interface IDbConnection
interface IDisposable
type OleDbConnection = class
inherit DbConnection
interface IDbConnection
interface IDisposable
interface ICloneable
Public NotInheritable Class OleDbConnection
Inherits DbConnection
Implements ICloneable, IDisposable
Public NotInheritable Class OleDbConnection
Inherits Component
Implements ICloneable, IDbConnection, IDisposable
Public NotInheritable Class OleDbConnection
Inherits DbConnection
Implements ICloneable
- 继承
- 继承
- 实现
示例
以下示例创建 OleDbCommand 和 OleDbConnection。 OleDbConnection打开 并设置为 Connection 的 OleDbCommand。 然后,该示例调用 ExecuteNonQuery 并关闭连接。 为此, ExecuteNonQuery 传递了一个连接字符串和一个作为 SQL INSERT 语句的查询字符串。
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 表示与数据源的唯一连接。 对于客户端/服务器数据库系统,它等效于与服务器的网络连接。 根据本机 OLE DB 访问接口支持的功能,对象的某些 OleDbConnection 方法或属性可能不可用。
创建 实例 OleDbConnection时,所有属性都设置为其初始值。 有关这些值的列表,请参阅 OleDbConnection 构造函数。
可以在单个 上 OleDbConnection打开多个 DataReader。 如果使用的 OLE DB 访问接口在单个连接上不支持多个 DataReader,则提供程序会为每个连接隐式打开一个附加连接。
OleDbConnection如果 超出范围,则不会关闭。 因此,必须通过调用 Close 或 Dispose 或使用 OleDbConnection 语句中的 Using
对象来显式关闭连接。
注意
若要部署高性能应用程序,必须使用连接池。 使用适用于 OLE DB 的 .NET Framework 数据提供程序时,不必启用连接池,因为提供程序会自动管理此连接池。 有关如何将连接池与 .NET Framework Data Provider for OLE DB 配合使用的详细信息,请参阅 OLE DB、ODBC 和 Oracle 连接池。
例如,如果致命 OleDbException (,则SQL Server严重性级别为 20 或更高) 由执行 OleDbCommand的方法生成,OleDbConnection则 可能会关闭 。 但是,用户可以重新打开连接并继续操作。
创建对象实例 OleDbConnection 的应用程序可以要求所有直接和间接调用方通过设置声明性或命令性安全要求来对代码具有足够的权限。 OleDbConnection 使用 OleDbPermission 对象提出安全要求。 用户可以使用 对象验证其代码是否具有足够的权限 OleDbPermissionAttribute 。 用户和管理员还可以使用 Caspol.exe (代码访问安全策略工具) 在计算机、用户和企业级别修改安全策略。 有关更多信息,请参见 Code Access Security and ADO.NET。
有关处理来自数据服务器的警告和信息性消息的详细信息,请参阅 连接事件。
注意
对象 OleDbConnection 不支持设置或检索特定于 OLE DB 访问接口的动态属性。 只支持可在 OLE DB 提供程序连接字符串中传递的属性。
构造函数
OleDbConnection() |
初始化 OleDbConnection 类的新实例。 |
OleDbConnection(String) |
使用指定的连接字符串初始化 OleDbConnection 类的新实例。 |
属性
CanCreateBatch |
获取一个值,该值指示此 DbConnection 是否支持 DbBatch 类。 (继承自 DbConnection) |
CanRaiseEvents |
获取一个指示组件是否可以引发事件的值。 (继承自 Component) |
ConnectionString |
获取或设置用于打数据库的字符串。 |
ConnectionTimeout |
获取在尝试建立连接时终止尝试并生成错误之前所等待的时间(以秒为单位)。 |
Container |
获取包含 IContainer 的 Component。 (继承自 Component) |
Database |
获取当前数据库的名称或打开连接后要使用的数据库的名称。 |
DataSource |
获取数据源的服务器名或文件名。 |
DbProviderFactory |
获取此 DbProviderFactory 的 DbConnection。 (继承自 DbConnection) |
DesignMode |
获取一个值,用以指示 Component 当前是否处于设计模式。 (继承自 Component) |
Events |
获取附加到此 Component 的事件处理程序的列表。 (继承自 Component) |
Provider |
获取在连接字符串的“Provider = ”子句中指定的 OLE DB 提供程序的名称。 |
ServerVersion |
获取一个包含客户端所连接到的服务器的版本的字符串。 |
Site | (继承自 Component) |
State |
获取连接的当前状态。 |
方法
事件
Disposed |
在通过调用 Dispose() 方法释放组件时发生。 (继承自 Component) |
InfoMessage |
在提供程序发送警告或信息性消息时发生。 |
StateChange |
当连接状态改变时发生。 |
StateChange |
当连接状态改变时发生。 (继承自 DbConnection) |
显式接口实现
ICloneable.Clone() |
有关此成员的说明,请参见 Clone()。 |
IDbConnection.BeginTransaction() |
此 API 支持产品基础结构,不能在代码中直接使用。 开始一项数据库事务。 |
IDbConnection.BeginTransaction() |
开始一项数据库事务。 (继承自 DbConnection) |
IDbConnection.BeginTransaction(IsolationLevel) |
此 API 支持产品基础结构,不能在代码中直接使用。 以指定的隔离级别启动数据库事务。 |
IDbConnection.BeginTransaction(IsolationLevel) |
以指定的隔离级别启动数据库事务。 (继承自 DbConnection) |
IDbConnection.CreateCommand() |
此 API 支持产品基础结构,不能在代码中直接使用。 创建并返回与连接关联的命令对象。 |
IDbConnection.CreateCommand() |
创建并返回与当前连接关联的 DbCommand 对象。 (继承自 DbConnection) |
扩展方法
ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可处置项返回的任务的等待。 |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈