1

我正在开发一个依赖 ADO.NET 实体数据模型的项目。我有 4 个相同的数据库模式,一个用于我公司的每个部门(营销、财务、行政和人力资源)。我目前知道我的每个用户所在的部门。我想使用部门来确定用户可以连接到哪个数据库。

在 C# 代码中,如何在运行时设置连接字符串?目前,我有

string connectionString = GetUsersConnectionString();
using (MyEntities entities = new MyEntities())
{
  MyDataEntity myDataEntity = new MyDataEntity();

  // Save to the database
  entities.MyDataEntity.Add(myDataEntity);
  entities.SaveChanges();
}

我在这里想念什么?如何设置MyEntitiesto的连接字符串connectionString

4

3 回答 3

1

这是一个很好的方法。

public static class ConnectionManager
    {
        public static string GetSqlConnectionString()
        {

            var serverName = @"" + ConfigurationManager.AppSettings["ServerName"];
            var databaseName = ConfigurationManager.AppSettings["DatabaseName"];
            var username = ConfigurationManager.AppSettings["Username"];
            var password = ConfigurationManager.AppSettings["Password"];

            SqlConnectionStringBuilder providerCs = new SqlConnectionStringBuilder();

            providerCs.DataSource = serverName;
            providerCs.InitialCatalog = databaseName;
            //providerCs.IntegratedSecurity = true;
            //providerCs.UserInstance = true;
            providerCs.UserID = username;
            providerCs.Password = password;
            var csBuilder = new EntityConnectionStringBuilder();

            csBuilder.Provider = "System.Data.SqlClient";
            csBuilder.ProviderConnectionString = providerCs.ToString();

            csBuilder.Metadata = string.Format("res://{0}/yourDataBase.csdl|res://{0}/yourDataBase.ssdl|res://{0}/yourDataBase.msl",
                typeof(yourDataBaseEntities).Assembly.FullName);

            return csBuilder.ToString();
        }
    }

用法:

在你的app.config地方这个代码

<appSettings>
    <add key="ServerName" value="UNKNOWN01-PC\sampleServer"/>
    <add key="DatabaseName" value="samplDatabase"/>
    <add key="Username" value="sampleUser"/>
    <add key="Password" value="sampPass"/>
</appSettings>

然后,

string connectionString = GetSqlConnectionString();
using (MyEntities entities = new MyEntities(connectionString))
{
  MyDataEntity myDataEntity = new MyDataEntity();

  // Save to the database
  entities.MyDataEntity.Add(myDataEntity);
  entities.SaveChanges();
}
于 2013-01-16T14:33:58.067 回答
0

利用:

string connectionString = GetUsersConnectionString();
using (MyEntities entities = new MyEntities(new EntityConnection(connectionString )))
{
  MyDataEntity myDataEntity = new MyDataEntity();

  // Save to the database
  entities.MyDataEntity.Add(myDataEntity);
  entities.SaveChanges();
}
于 2013-01-16T14:11:23.360 回答
0

在您的 Model.Designer.cs 文件中,您必须初始化一个新的实体对象,该对象采用所需数量的参数。例如:

namespace:MyModel
{
#region Contexts 
public partial class MyEntities : ObjectContext
{
    #region Constructors

public MyEntities(EntityConnection connection) //add the required arguments
        : base(connection, "Entities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }
....

然后使用您的新实体来操作连接字符串检索到的任何数据,例如

 MyModel.MyEntities db = new MyModel.Entities(connectionString);
 var query = from rows in db.Table_Name orderby rows.ID select rows; 
于 2013-08-13T01:36:13.020 回答