OleDbConnection 构造函数

定义

初始化 OleDbConnection 类的新实例。Initializes a new instance of the OleDbConnection class.

重载

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.

OleDbConnection()

初始化 OleDbConnection 类的新实例。Initializes a new instance of the OleDbConnection class.

public:
 OleDbConnection();
public OleDbConnection ();
Public Sub New ()

示例

下面的示例创建并打开 OleDbConnectionThe following example creates and opens an OleDbConnection.

static void OpenConnection(string connectionString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        try
        {
            connection.Open();
            Console.WriteLine("DataSource: {0} \nDatabase: {1}",
                connection.DataSource, connection.Database);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
}
Public Sub OpenConnection(ByVal connectionString As String)

    Using connection As New OleDbConnection(connectionString)
        Try
            connection.Open()
            Console.WriteLine("DataSource: {0} Database: {1}", _
                connection.DataSource, connection.Database)
        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 的新实例时,读/写属性将设置为以下初始值,除非它们是在 ConnectionString 属性中使用其关联关键字特别设置的。When a new instance of OleDbConnection is created, the read/write properties are set to the following initial values unless they are specifically set using their associated keywords in the ConnectionString property.

属性Properties 初始值Initial value
ConnectionString 空字符串("")empty string ("")
ConnectionTimeout 1515
Database 空字符串("")empty string ("")
DataSource 空字符串("")empty string ("")

只能使用 "ConnectionString" 属性更改这些属性的值。You can change the value for these properties only by using the ConnectionString property.

另请参阅

OleDbConnection(String)

使用指定的连接字符串初始化 OleDbConnection 类的新实例。Initializes a new instance of the OleDbConnection class with the specified connection string.

public:
 OleDbConnection(System::String ^ connectionString);
public OleDbConnection (string connectionString);
new System.Data.OleDb.OleDbConnection : string -> System.Data.OleDb.OleDbConnection
Public Sub New (connectionString As String)

参数

connectionString
String

用于打开数据库的连接。The connection used to open the database.

示例

下面的示例创建并打开 OleDbConnectionThe following example creates and opens an OleDbConnection.

static void OpenConnection(string connectionString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        try
        {
            connection.Open();
            Console.WriteLine("DataSource: {0} \nDatabase: {1}",
                connection.DataSource, connection.Database);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
}
Public Sub OpenConnection(ByVal connectionString As String)

    Using connection As New OleDbConnection(connectionString)
        Try
            connection.Open()
            Console.WriteLine("DataSource: {0} Database: {1}", _
                connection.DataSource, connection.Database)
        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 的新实例时,读/写属性将设置为以下初始值,除非它们是在 ConnectionString 属性中使用其关联关键字特别设置的。When a new instance of OleDbConnection is created, the read/write properties are set to the following initial values unless they are specifically set using their associated keywords in the ConnectionString property.

属性Properties 初始值Initial value
ConnectionString connectionString
ConnectionTimeout 1515
Database 空字符串("")empty string ("")
DataSource 空字符串("")empty string ("")

只能使用 "ConnectionString" 属性更改这些属性的值。You can change the value for these properties only by using the ConnectionString property.

另请参阅

适用于