OleDbConnectionStringBuilder.DataSource 属性

定义

获取或设置要连接到的数据源的名称。

public:
 property System::String ^ DataSource { System::String ^ get(); void set(System::String ^ value); };
public string DataSource { get; set; }
member this.DataSource : string with get, set
Public Property DataSource As String

属性值

DataSource 属性的值,或者,如果未提供任何值,则为 String.Empty

示例

以下示例控制台应用程序创建一个新 OleDbConnectionStringBuilder 实例,然后设置 DataSourceProvider 属性。 最后,该示例创建一个新 OleDbConnection 实例,并使用该对象连接到指示的数据存储。

using System.Data.OleDb;

class Program
{
    static void Main()
    {
        OleDbConnectionStringBuilder builder =
            new OleDbConnectionStringBuilder();
        builder.DataSource = @"C:\Sample.mdb";
        builder.Provider = "Microsoft.Jet.Oledb.4.0";
        Console.WriteLine(builder.ConnectionString);

        // This sample assumes that you have a database named
        // C:\Sample.mdb available.
        using (OleDbConnection connection = new
                   OleDbConnection(builder.ConnectionString))
        {
            try
            {
                connection.Open();
                // Do something with the database here.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        Console.WriteLine("Press Enter to finish.");
        Console.ReadLine();
    }
}
Imports System.Data.OleDb

Module Module1
    Sub Main()
        Dim builder As New OleDbConnectionStringBuilder()
        builder.DataSource = "C:\Sample.mdb"
        builder.Provider = "Microsoft.Jet.Oledb.4.0"
        Console.WriteLine(builder.ConnectionString)

        ' This sample assumes that you have a database named
        ' C:\Sample.mdb available.
        Using connection As New OleDbConnection(builder.ConnectionString)
            Try
                connection.Open()
                ' Do something with the database here.
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using

        Console.WriteLine("Press Enter to finish.")
        Console.ReadLine()
    End Sub
End Module

注解

如果在您尝试设置此属性时传入的值为 null,则将重置 DataSource 属性。 如果尚未设置该值,并且开发人员尝试检索属性,则返回值为 String.Empty。 此属性对应于连接字符串中的“数据源”键。

适用于

另请参阅