OleDbConnection.DataSource OleDbConnection.DataSource OleDbConnection.DataSource OleDbConnection.DataSource Property

定义

获取数据源的服务器名或文件名。Gets the server name or file name of the data source.

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

属性值

数据源的服务器名或文件名。The server name or file name of the data source. 默认值为一个空字符串。The default value is an empty string.

示例

下面的示例创建OleDbConnection并显示其某些只读属性。The following example creates an OleDbConnection and displays some of its read-only properties.

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包含的任何内容。If the connection to the database is closed, the DataSource property returns whatever is contained in ConnectionString for the data source keyword. 如果连接处于打开状态,并且ConnectionString数据源关键字的值以 "|datadirectory|" 开头,则属性仅返回数据源关键字ConnectionString中包含的任何内容。If the connection is open and the ConnectionString data source keyword's value starts with "|datadirectory|", the property returns whatever is contained in ConnectionString for the data source keyword only. 如果与数据库的连接已打开,则属性返回本机提供程序为DBPROP_INIT_DATASOURCE,提供的,如果为空,则返回本机提供程序的。 DBPROP_DATASOURCENAMEIf the connection to the database is open, the property returns what the native provider returns for the DBPROP_INIT_DATASOURCE, and if that is empty, the native provider's DBPROP_DATASOURCENAME is returned.

适用于

另请参阅