共用方式為


OleDbConnection 建構函式

定義

初始化 OleDbConnection 類別的新執行個體。

多載

OleDbConnection()

初始化 OleDbConnection 類別的新執行個體。

OleDbConnection(String)

使用指定的連接字串,初始化 OleDbConnection 類別的新執行個體。

OleDbConnection()

來源:
DbConnectionHelper.cs
來源:
DbConnectionHelper.cs
來源:
DbConnectionHelper.cs

初始化 OleDbConnection 類別的新執行個體。

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

範例

下列範例會建立並開啟 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 其相關聯的關鍵詞進行特別設定。

屬性 初始值
ConnectionString 空字串 (“”)
ConnectionTimeout 15
Database 空字串 (“”)
DataSource 空字串 (“”)

您只能使用 ConnectionString 屬性來變更這些屬性的值。

另請參閱

適用於

OleDbConnection(String)

來源:
OleDbConnection.cs
來源:
OleDbConnection.cs
來源:
OleDbConnection.cs

使用指定的連接字串,初始化 OleDbConnection 類別的新執行個體。

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

參數

connectionString
String

用來開啟資料庫的連接。

範例

下列範例會建立並開啟 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 其相關聯的關鍵詞進行特別設定。

屬性 初始值
ConnectionString connectionString
ConnectionTimeout 15
Database 空字串 (“”)
DataSource 空字串 (“”)

您只能使用 ConnectionString 屬性來變更這些屬性的值。

另請參閱

適用於