EntityConnectionStringBuilder 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 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
是否有效关键字 (keyword) /值对。 有关有效的关键字/值语法,请参见 ConnectionString。
必须在 参数中 connectionString
指定固定提供程序名称。 支持的提供程序包括:
System.Data.Odbc
System.Data.OleDb
System.Data.OracleClient
System.Data.SqlClient
Microsoft.SqlServerCe.Client