OleDbConnection 类

表示到数据源的连接是打开的。

**命名空间:**System.Data.OleDb
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public NotInheritable Class OleDbConnection
    Inherits DbConnection
    Implements ICloneable, IDbConnection, IDisposable
用法
Dim instance As OleDbConnection
public sealed class OleDbConnection : DbConnection, ICloneable, IDbConnection, IDisposable
public ref class OleDbConnection sealed : public DbConnection, ICloneable, IDbConnection, IDisposable
public final class OleDbConnection extends DbConnection implements ICloneable, IDbConnection, 
    IDisposable
public final class OleDbConnection extends DbConnection implements ICloneable, IDbConnection, 
    IDisposable

备注

一个 OleDbConnection 对象,表示到数据源的一个唯一的连接。在客户端/服务器数据库系统中,它等效于一个到服务器的网络连接。OleDbConnection 对象的某些方法或属性可能不可用,这取决于本机 OLE DB 提供程序所支持的功能。

当创建 OleDbConnection 的实例时,所有属性都设置为它们的初始值。有关这些值的列表,请参见 OleDbConnection 构造函数。

如果 OleDbConnection 超出范围,则不会将其关闭。因此,必须通过调用 CloseDispose,或通过在 Using 语句中使用 OleDbConnection 对象来显式关闭此连接。

提示

若要部署高性能应用程序,则必须使用连接池。当使用用于 OLE DB 的 .NET Framework 数据提供程序时,不必启用连接池,因为这由提供程序自动管理。有关如何配合使用连接池与用于 OLE DB 的 .NET Framework 数据提供程序的更多信息,请参见 了解连接池

如果执行 OleDbCommand 的方法生成严重的 OleDbException(例如,SQL Server 严重级别等于或大于 20),OleDbConnection 可能会关闭。但是,用户可以重新打开连接并继续操作。

创建 OleDbConnection 对象的实例的应用程序可通过设置声明性或强制性安全要求,要求所有直接和间接的调用方对代码都具有足够的权限。OleDbConnection 使用 OleDbPermission 对象设置安全要求。用户可以通过使用 OleDbPermissionAttribute 对象来验证他们的代码是否具有足够的权限。用户和管理员还可以使用 代码访问安全策略工具 (Caspol.exe) 来修改计算机、用户和企业级别的安全策略。有关更多信息,请参见 代码访问安全性和 ADO.NET

有关处理来自数据服务器的警告和信息性消息的更多信息,请参见 使用连接事件

提示

OleDbConnection 对象不支持设置或检索特定于 OLE DB 提供程序的动态属性。只支持可在 OLE DB 提供程序的连接字符串中传递的属性。

示例

下面的示例创建一个 OleDbCommand 和一个 OleDbConnectionOleDbConnection 打开,并设置为 OleDbCommandConnection。然后,该示例调用 ExecuteNonQuery 并关闭该连接。若要完成此任务,请为 ExecuteNonQuery 传递一个连接字符串和一个查询字符串,后者是一个 SQL INSERT 语句。

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
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.
    }
using System;
using System.Data;
using System.Data.OleDb;

class Class1
{
    static void Main()
    {
    }

    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.
        }

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Data.Common.DbConnection
        System.Data.OleDb.OleDbConnection

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

OleDbConnection 成员
System.Data.OleDb 命名空间

其他资源

连接到数据源