共用方式為


EntityConnectionStringBuilder 建構函式

定義

初始化 EntityConnectionStringBuilder 類別的新執行個體。

多載

EntityConnectionStringBuilder()

初始化 EntityConnectionStringBuilder 類別的新執行個體。

EntityConnectionStringBuilder(String)

使用提供的連接字串初始化 EntityConnectionStringBuilder 類別的新執行個體。

EntityConnectionStringBuilder()

初始化 EntityConnectionStringBuilder 類別的新執行個體。

public:
 EntityConnectionStringBuilder();
public EntityConnectionStringBuilder ();
Public Sub New ()

範例

下列範例示範如何搭配 EntityConnectionStringBuilder 使用 SqlConnectionStringBuilder。 程式代碼會設定 的屬性SqlConnectionStringBuilder,以建立 SqlConnection 連接字串,以提供基礎提供者 連接字串 的一部分。 請注意,無法使用 設定提供者名稱,SqlConnectionStringBuilder因為它無效 SqlConnection 連接字串 語法。 程式代碼會藉由設定EntityConnectionStringBuilder屬性來EntityConnection建立 連接字串。


// Specify the provider name, server and database.
string providerName = "System.Data.SqlClient";
string serverName = ".";
string databaseName = "AdventureWorks";

// Initialize the connection string builder for the
// underlying provider.
SqlConnectionStringBuilder sqlBuilder =
    new SqlConnectionStringBuilder();

// Set the properties for the data source.
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
sqlBuilder.IntegratedSecurity = true;

// Build the SqlConnection connection string.
string providerString = sqlBuilder.ToString();

// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder =
    new EntityConnectionStringBuilder();

//Set the provider name.
entityBuilder.Provider = providerName;

// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;

// Set the Metadata location.
entityBuilder.Metadata = @"res://*/AdventureWorksModel.csdl|
                            res://*/AdventureWorksModel.ssdl|
                            res://*/AdventureWorksModel.msl";
Console.WriteLine(entityBuilder.ToString());

using (EntityConnection conn =
    new EntityConnection(entityBuilder.ToString()))
{
    conn.Open();
    Console.WriteLine("Just testing the connection.");
    conn.Close();
}
' Specify the provider name, server and database.
Dim providerName As String = "System.Data.SqlClient"
Dim serverName As String = "."
Dim databaseName As String = "AdventureWorks"

' Initialize the connection string builder for the
' underlying provider.
Dim sqlBuilder As New SqlConnectionStringBuilder

' Set the properties for the data source.
sqlBuilder.DataSource = serverName
sqlBuilder.InitialCatalog = databaseName
sqlBuilder.IntegratedSecurity = True

' Build the SqlConnection connection string.
Dim providerString As String = sqlBuilder.ToString

' Initialize the EntityConnectionStringBuilder.
Dim entityBuilder As New EntityConnectionStringBuilder

'Set the provider name.
entityBuilder.Provider = providerName
' Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString
' Set the Metadata location to the current directory.
entityBuilder.Metadata = "res://*/AdventureWorksModel.csdl|" & _
                            "res://*/AdventureWorksModel.ssdl|" & _
                            "res://*/AdventureWorksModel.msl"

Console.WriteLine(entityBuilder.ToString)

Using conn As EntityConnection = New EntityConnection(entityBuilder.ToString)
    conn.Open()
    Console.WriteLine("Just testing the connection.")
    conn.Close()
End Using

另請參閱

適用於

EntityConnectionStringBuilder(String)

使用提供的連接字串初始化 EntityConnectionStringBuilder 類別的新執行個體。

public:
 EntityConnectionStringBuilder(System::String ^ connectionString);
public EntityConnectionStringBuilder (string connectionString);
new System.Data.EntityClient.EntityConnectionStringBuilder : string -> System.Data.EntityClient.EntityConnectionStringBuilder
Public Sub New (connectionString As String)

參數

connectionString
String

基礎資料來源的提供者特有連接字串。

範例

下列範例示範如何搭配 EntityConnectionStringBuilder 使用 SqlConnectionStringBuilder。 程式代碼會設定 的屬性SqlConnectionStringBuilder,以建立 SqlConnection 連接字串,以提供基礎提供者 連接字串 的一部分。 請注意,無法使用 設定提供者名稱,SqlConnectionStringBuilder因為它無效 SqlConnection 連接字串 語法。 程式代碼會藉由設定EntityConnectionStringBuilder屬性來EntityConnection建立 連接字串。 它不會使用 connectionString 多載。


// Specify the provider name, server and database.
string providerName = "System.Data.SqlClient";
string serverName = ".";
string databaseName = "AdventureWorks";

// Initialize the connection string builder for the
// underlying provider.
SqlConnectionStringBuilder sqlBuilder =
    new SqlConnectionStringBuilder();

// Set the properties for the data source.
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
sqlBuilder.IntegratedSecurity = true;

// Build the SqlConnection connection string.
string providerString = sqlBuilder.ToString();

// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder =
    new EntityConnectionStringBuilder();

//Set the provider name.
entityBuilder.Provider = providerName;

// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;

// Set the Metadata location.
entityBuilder.Metadata = @"res://*/AdventureWorksModel.csdl|
                            res://*/AdventureWorksModel.ssdl|
                            res://*/AdventureWorksModel.msl";
Console.WriteLine(entityBuilder.ToString());

using (EntityConnection conn =
    new EntityConnection(entityBuilder.ToString()))
{
    conn.Open();
    Console.WriteLine("Just testing the connection.");
    conn.Close();
}
' Specify the provider name, server and database.
Dim providerName As String = "System.Data.SqlClient"
Dim serverName As String = "."
Dim databaseName As String = "AdventureWorks"

' Initialize the connection string builder for the
' underlying provider.
Dim sqlBuilder As New SqlConnectionStringBuilder

' Set the properties for the data source.
sqlBuilder.DataSource = serverName
sqlBuilder.InitialCatalog = databaseName
sqlBuilder.IntegratedSecurity = True

' Build the SqlConnection connection string.
Dim providerString As String = sqlBuilder.ToString

' Initialize the EntityConnectionStringBuilder.
Dim entityBuilder As New EntityConnectionStringBuilder

'Set the provider name.
entityBuilder.Provider = providerName
' Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString
' Set the Metadata location to the current directory.
entityBuilder.Metadata = "res://*/AdventureWorksModel.csdl|" & _
                            "res://*/AdventureWorksModel.ssdl|" & _
                            "res://*/AdventureWorksModel.msl"

Console.WriteLine(entityBuilder.ToString)

Using conn As EntityConnection = New EntityConnection(entityBuilder.ToString)
    conn.Open()
    Console.WriteLine("Just testing the connection.")
    conn.Close()
End Using

備註

提供的 connectionString 不會檢查有效的關鍵詞/值組。 如需有效關鍵字/值的語法,請參閱 ConnectionString

必須在 參數中 connectionString 指定非變異提供者名稱。 支援的提供者包括下列:

  • System.Data.Odbc

  • System.Data.OleDb

  • System.Data.OracleClient

  • System.Data.SqlClient

  • Microsoft.SqlServerCe.Client

另請參閱

適用於