使EntityFramework连接字符串动态 [英] Making EntityFramework Connection String dynamic
本文介绍了使EntityFramework连接字符串动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
//指定提供商名称,服务器和数据库。
string providerName =System.Data.SqlClient;
string serverName =VENUS-PC;
string databaseName =Check;
//初始化
//底层提供程序的连接字符串构建器。
SqlConnectionStringBuilder sqlBuilder =
new SqlConnectionStringBuilder();
//设置数据源的属性。
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
//sqlBuilder.IntegratedSecurity = true;
sqlBuilder.UserID =sa;
sqlBuilder.Password =123;
sqlBuilder.MultipleActiveResultSets = true;
//构建SqlConnection连接字符串。
string providerString = sqlBuilder.ToString();
//初始化EntityConnectionStringBuilder。
EntityConnectionStringBuilder entityBuilder =
new EntityConnectionStringBuilder();
//设置提供者名称。
entityBuilder.Provider = providerName;
//设置特定于提供者的连接字符串。
entityBuilder.ProviderConnectionString = providerString;
//设置元数据位置。
entityBuilder.Metadata = @res://*/Cheque.csdl |
res://*/Cheque.ssdl |
res://*/Cheque.msl;
//Console.WriteLine(entityBuilder.ToString());
System.Windows.Forms.MessageBox.Show(entityBuilder.ToString());
using(EntityConnection conn =
new EntityConnection(entityBuilder.ToString()))
{
conn.Open();
//Console.WriteLine(Just testing the connection。);
System.Windows.Forms.MessageBox.Show(Connection is Ok);
conn.Close();
}
但此异常抛出:底层提供程序在打开时失败。登录失败的用户山。
我在Sql Server Managment Studio中测试用户名,密码,服务器名称和数据库名称,它正在运行。
我如何修复我的代码?
解决方案
public static string GetConStrSQL )
{
string connectionString = new System.Data.EntityClient.EntityConnectionStringBuilder
{
元数据=res:// *,
Provider =System.Data .SqlClient,
ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
{
InitialCatalog =数据库名称,
DataSource = @ServerNameHere\SQLEXPRESS
IntegratedSecurity = false,
UserID =sa,
Password =your_password_here,
} .ConnectionString
} .ConnectionString;
return connectionString;
}
并创建上下文:
YouEFContext ctx = new YouEFContext(GetConStrSQL());
I'm using this code for connect to my database over the network :
// Specify the provider name, server and database.
string providerName = "System.Data.SqlClient";
string serverName = "VENUS-PC";
string databaseName = "Cheque";
// 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;
sqlBuilder.UserID = "sa";
sqlBuilder.Password = "123";
sqlBuilder.MultipleActiveResultSets = 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://*/Cheque.csdl|
res://*/Cheque.ssdl|
res://*/Cheque.msl";
//Console.WriteLine(entityBuilder.ToString());
System.Windows.Forms.MessageBox.Show(entityBuilder.ToString());
using (EntityConnection conn =
new EntityConnection(entityBuilder.ToString()))
{
conn.Open();
//Console.WriteLine("Just testing the connection.");
System.Windows.Forms.MessageBox.Show("Connection is Ok");
conn.Close();
}
But this exception Thrown : The underlying provider failed on open.Login Failed for user 'sa'. And I test the user name ,password ,server name and database name in Sql Server Managment Studio and it's working. How can i fix my code?
解决方案
public static string GetConStrSQL()
{
string connectionString = new System.Data.EntityClient.EntityConnectionStringBuilder
{
Metadata = "res://*",
Provider = "System.Data.SqlClient",
ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
{
InitialCatalog = "Name Of The Database",
DataSource = @"ServerNameHere\SQLEXPRESS",
IntegratedSecurity = false,
UserID = "sa",
Password = "your_password_here",
}.ConnectionString
}.ConnectionString;
return connectionString;
}
and in the creation of the context :
YouEFContext ctx = new YouEFContext(GetConStrSQL());
这篇关于使EntityFramework连接字符串动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文