共用方式為


OleDbConnection.ConnectionString 屬性

定義

取得或設定用來開啟資料庫的字串。

public:
 virtual property System::String ^ ConnectionString { System::String ^ get(); void set(System::String ^ value); };
public:
 property System::String ^ ConnectionString { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.SettingsBindable(true)]
public override string ConnectionString { get; set; }
[System.Data.DataSysDescription("OleDbConnection_ConnectionString")]
public string ConnectionString { get; set; }
public override string ConnectionString { get; set; }
[<System.ComponentModel.SettingsBindable(true)>]
member this.ConnectionString : string with get, set
[<System.Data.DataSysDescription("OleDbConnection_ConnectionString")>]
member this.ConnectionString : string with get, set
member this.ConnectionString : string with get, set
Public Overrides Property ConnectionString As String
Public Property ConnectionString As String

屬性值

包含資料來源名稱及其他建立初始連線所需的參數的 OLE DB 提供者連線字串。 預設值為空白字串。

實作

屬性

例外狀況

提供無效的連接字串參數,或未提供所需的連接字串參數。

範例

以下範例建立 , OleDbConnection 並在連接字串中設定其部分屬性。

static void OpenConnection(string connectionString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        try
        {
            connection.Open();
            Console.WriteLine("ServerVersion: {0} \nDataSource: {1}",
                connection.ServerVersion, connection.DataSource);
        }
        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("Server Version: {0} DataSource: {1}", _
                connection.ServerVersion, connection.DataSource)
        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

備註

ConnectionString設計上盡可能接近 OLE DB 的連接字串格式,但有以下例外:

  • 「提供者 = value 」條款是必須的。 然而,你不能使用「Provider = MSDASQL」,因為 OLE DB 的 .NET Framework 資料提供者不支援 ODBC 的 OLE DB 提供者(MSDASQL)。 要存取 ODBC 資料來源,請使用 OdbcConnection 命名空間中的 System.Data.Odbc 物件。

  • 與 ODBC 或 ADO 不同,回傳的連接字串與使用者設定ConnectionString的 相同,若設定為false(預設值),則減去安全資訊Persist Security Info。 OLE DB 的 .NET Framework 資料提供者不會持久化或回傳密碼,除非你將關鍵字設 Persist Security Info 為( true 不建議)。 為了維持高安全性,強烈建議使用將 Persist Security Info 設定為 false的關鍵字。Integrated Security

你可以利用這個 ConnectionString 屬性連接各種資料來源。 以下範例說明了幾種可能的連接串。

"Provider=MSDAORA; Data Source=ORACLE8i7;Persist Security Info=False;Integrated Security=Yes"

"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\bin\LocalAccess40.mdb"

"Provider=SQLOLEDB;Data Source=(local);Integrated Security=SSPI"

如果 Data Source 關鍵字未在連接字串中指定,提供者會嘗試連接到本地伺服器(若有伺服器可用)。

欲了解更多關於連接字串的資訊,請參閱「 使用 SQL Server Native Client 的連接字串關鍵字」。

ConnectionString該屬性只能在連線關閉時設定。 許多連接字串值具有相應的唯讀特性。 當連接字串被設定時,這些屬性會被更新,除非偵測到錯誤。 在這種情況下,所有屬性都沒有更新。 OleDbConnection 屬性僅回傳包含在 ConnectionString中的設定。

在封閉連接上重置 會 ConnectionString 重置所有連接字串值及相關屬性。 這也包括密碼。 例如,如果你設定一個連接字串包含「Initial Catalog= AdventureWorks」,然後將連接字串重設為「Provider= SQLOLEDB;資料來源= MySQLServer;IntegratedSecurity=SSPI」,該 Database 屬性不再設定為 AdventureWorks。 (連接字串的初始目錄值對應於屬性 Database 。)

當該屬性被設定時,會對連接字串進行初步驗證。 若字串中包含 、 Connect TimeoutPersist Security InfoOLE DB Services 的值Provider,則會檢查這些值。 當應用程式呼叫該 Open 方法時,連接字串會被完全驗證。 若連接字串包含無效或不支援的屬性,會產生執行時例外,例如 ArgumentException

謹慎

可以在通用資料連結(UDL)檔案中為OleDbConnection提供連線資訊,但應避免這麼做。 UDL 檔案未加密,且以明文形式暴露連線字串資訊。 因為對您的應用程式而言,UDL 檔是外部的檔案型資源,所以您無法使用 .NET Framework 來保護該檔案。

連接字串的基本格式包含一系列以分號分隔的關鍵字/值對。 等號(=)連接每個關鍵字及其值。 若要包含分號、單引號或雙引號的值,該值必須以雙引號包圍。 若值同時包含分號與雙引號,則可用單引號包覆。 如果值以雙引號字元開頭,單引號也很有用。 反之,若值以單一引號開頭,則可使用雙引號。 若值同時包含單引號與雙引號字元,則每次該值內出現引號時,用來包圍該值的引號字元都必須加倍。

若要在字串值中包含前後空格,該值必須以單引號或雙引號包圍。 整數、布林或列舉值周圍的前置或後置空格都會被忽略,即使被引號包圍。 然而,字串字面關鍵字或值中的空格會被保留。 單引號或雙引號可在連接字串中使用,且不使用分隔符(例如, Data Source= my'ServerData Source= my"Server),除非引號字元是該值的第一個或最後一個字元。

要在關鍵字或值中包含等號(=),必須在前方加上另一個等號。 例如,在以下假設的連接字串中,關鍵字為「key=word」,值為「value」。

"key==word=value"

如果關鍵字=值對中的特定關鍵字在連接字串中出現多次,則該值集合會使用最後一次出現的那個。

關鍵字不區分大小寫。

謹慎

在根據使用者輸入構建連接字串時,例如從對話框取得使用者 ID 和密碼資訊並將其附加到連接字串時,應特別小心。 應用程式應確保使用者無法在這些值中嵌入額外的連接字串參數,例如輸入密碼為「validpassword;database= somedb」,試圖附加到另一個資料庫。 如果你使用 OLE DB 連線的 Extended Properties 連線字串參數,請避免傳遞使用者 ID 和密碼,因為你應該盡量避免以明文方式儲存使用者 ID 和密碼,且預設Persist Security Info= false設定不會影響參數。Extended Properties

適用於

另請參閱