OleDbConnectionStringBuilder.DataSource 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定要連接的目標資料來源名稱。
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 實例,然後設定 DataSource 和 Provider 屬性。 最後,此範例會建立新的 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
。 此屬性會對應至 連接字串 內的「數據源」索引鍵。