共用方式為


OleDbConnection.DataSource 屬性

定義

取得資料來源的伺服器名稱或檔案名稱。

public:
 virtual property System::String ^ DataSource { System::String ^ get(); };
public:
 property System::String ^ DataSource { System::String ^ get(); };
[System.ComponentModel.Browsable(true)]
public override string DataSource { get; }
[System.Data.DataSysDescription("OleDbConnection_DataSource")]
public string DataSource { get; }
[<System.ComponentModel.Browsable(true)>]
member this.DataSource : string
[<System.Data.DataSysDescription("OleDbConnection_DataSource")>]
member this.DataSource : string
Public Overrides ReadOnly Property DataSource As String
Public ReadOnly Property DataSource As String

屬性值

資料來源的伺服器名稱或檔案名稱。 預設值為空字串。

屬性

範例

下列範例會建立 , 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

備註

如果資料庫連接已關閉, DataSource 則 屬性會傳回數據源關鍵詞中包含的 ConnectionString 任何專案。 如果連接已開啟,且 ConnectionString 數據源關鍵詞的值以 “|datadirectory|” 開頭,則 屬性只會傳回數據源關鍵詞中包含的 ConnectionString 任何內容。 如果資料庫連接已開啟,則 屬性會傳回 DBPROP_INIT_DATASOURCE, 的原生提供者傳回的內容,如果這是空的,則會傳回原生提供者的 DBPROP_DATASOURCENAME

適用於

另請參閱