我有一些SQL Server对于存储的数据是相同的,我希望能够在它们之间进行更改,如果我添加更多,我想轻松添加它们与Windows窗体。
我首先完成了一个数据库,这是添加到App.config文件的连接字符串。出于安全原因,我更改了用户名和密码
data.label
有没有一种简单的方法来改变我应该从哪里获取数据? 我正在考虑使用一个组合框,我可以选择从哪个SQL Server获取数据。 如果我手动更改连接字符串,它的工作原理。但我怎么用代码呢?
答案 0 :(得分:1)
您可以使用类EntityConnectionStringBuilder来构建连接字符串。请参阅此处https://msdn.microsoft.com/en-us/library/orm-9780596520281-01-16.aspx和Programmatic Connection Strings in Entity Framework 6
// 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();
}
答案 1 :(得分:0)
我做了什么让它发挥作用。 在上下文文件中,我更改了
public MigrateDBFaktura3Entities ()
: base("name=MigrateDBFaktura3Entities")
{
}
到
public MigrateDBFaktura3Entities (string connectionString)
: base(connectionString)
{
}
然后我做了一个HelperClass
class ConnectionHelper
{
public static string CreateConnectionString(LocationModel LM, string metaData)
{
const string appName = "EntityFramework";
const string providerName = "System.Data.SqlClient";
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder.DataSource = LM.datasource;
sqlBuilder.InitialCatalog = LM.catalog;
sqlBuilder.UserID = LM.Username;
sqlBuilder.Password = LM.Password;
sqlBuilder.MultipleActiveResultSets = true;
sqlBuilder.PersistSecurityInfo = true;
sqlBuilder.ApplicationName = appName;
EntityConnectionStringBuilder efBuilder = new EntityConnectionStringBuilder();
efBuilder.Metadata = metaData;
efBuilder.Provider = providerName;
efBuilder.ProviderConnectionString = sqlBuilder.ConnectionString;
var t = efBuilder.ConnectionString;
return efBuilder.ConnectionString;
}
public static FastecData CreateConnection(LocationModel locationmodel, string metaData = "res://*/DB.ServerData.csdl|res://*/DB.ServerData.ssdl|res://*/DB.ServerData.msl")
{
return new FastecData(ConnectionHelper.CreateConnectionString(locationmodel, metaData));
}
}
LocationModel是数据库,它纯粹包含我将连接到的不同服务器的数据以从中获取数据。
然后,当我需要连接到它时,我只需要
MigrateDBFaktura3Entities db = ConnectionHelper.CreateConnection(CurrentLocation)
其中CurrentLocation是LocationModel