共用方式為


SqlConnection.DataSource 屬性

定義

取得要連線的 SQL Server 執行個體名稱。

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

屬性值

要連線之 SQL Server 實例的名稱。 預設值為空字串。

屬性

範例

下列範例會建立 , SqlConnection 並顯示其部分唯讀屬性。

private static void OpenSqlConnection(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("DataSource: {0}", connection.DataSource);
    }
}
Private Sub OpenSqlConnection(ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        connection.Open()
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion)
        Console.WriteLine("DataSource: {0}", connection.DataSource)
    End Using
End Sub

備註

注意

如果 的 SqlConnection 連接字串 為 “context connection=true”,則DataSource屬性會傳回 null

適用於

另請參閱