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 属性将返回数据源关键字 (keyword) 中包含的ConnectionString任何内容。 如果连接处于打开状态,ConnectionString并且数据源关键字 (keyword) 的值以“|datadirectory|”开头,则 属性仅返回数据源关键字 (keyword) 中包含的ConnectionString任何内容。 如果与数据库的连接处于打开状态,则 属性返回本机提供程序为 DBPROP_INIT_DATASOURCE, 返回的内容;如果为空,则返回本机提供程序的 DBPROP_DATASOURCENAME

适用于

另请参阅