共用方式為


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。 此屬性會對應至 連接字串 內的「數據源」索引鍵。

適用於

另請參閱