Szerkesztés

Megosztás a következőn keresztül:


EntityConnectionStringBuilder Constructors

Definition

Initializes a new instance of the EntityConnectionStringBuilder class.

Overloads

EntityConnectionStringBuilder()

Initializes a new instance of the EntityConnectionStringBuilder class.

EntityConnectionStringBuilder(String)

Initializes a new instance of the EntityConnectionStringBuilder class using the supplied connection string.

EntityConnectionStringBuilder()

Initializes a new instance of the EntityConnectionStringBuilder class.

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

Examples

The following example demonstrates how to use the EntityConnectionStringBuilder in conjunction with a SqlConnectionStringBuilder. The code sets properties of a SqlConnectionStringBuilder to create a SqlConnection connection string that supplies part of the underlying provider connection string. Note that the Provider name cannot be set using the SqlConnectionStringBuilder because it is not valid SqlConnection connection string syntax. The code creates the EntityConnection connection string by setting EntityConnectionStringBuilder properties.


// 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

See also

Applies to

EntityConnectionStringBuilder(String)

Initializes a new instance of the EntityConnectionStringBuilder class using the supplied connection string.

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)

Parameters

connectionString
String

A provider-specific connection string to the underlying data source.

Examples

The following example demonstrates how to use the EntityConnectionStringBuilder in conjunction with a SqlConnectionStringBuilder. The code sets properties of a SqlConnectionStringBuilder to create a SqlConnection connection string that supplies part of the underlying provider connection string. Note that the Provider name cannot be set using the SqlConnectionStringBuilder because it is not valid SqlConnection connection string syntax. The code creates the EntityConnection connection string by setting EntityConnectionStringBuilder properties. It does not use the connectionString overload.


// 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

Remarks

The supplied connectionString is not checked for valid keyword/value pairs. For valid keyword/value syntax, see ConnectionString.

The invariant Provider name must be specified in the connectionString parameter. Supported providers include the following:

  • System.Data.Odbc
  • System.Data.OleDb
  • System.Data.OracleClient
  • System.Data.SqlClient
  • Microsoft.SqlServerCe.Client

See also

Applies to