在C#中以编程方式更新连接字符串后重新加载web.config

时间:2015-05-28 09:38:45

标签: c# asp.net entity-framework

我在运行时在web.config中创建了Connectionstring。现在我将一些数据保存到我的数据库中,但它仍在读取旧的Connectionstring。我怎么解决这个问题?下面是我的代码:

public string EditConnectionString(string userId, string password, string host, string dbName)
{
    try
    {
        System.Configuration.Configuration Config = WebConfigurationManager.OpenWebConfiguration("~");
        ConnectionStringsSection conSetting = (ConnectionStringsSection)Config.GetSection("connectionStrings");
        string providerName = "System.Data.EntityClient";
        string conString = @"metadata=res://*/OWordpress.csdl|res://*/OWordpress.ssdl|res://*/OWordpress.msl;provider=System.Data.SqlClient;provider connection string=" + "\"data source=" + host + ";initial catalog=" + dbName + ";user id=" + userId + ";password=" + password + ";MultipleActiveResultSets=True;App=EntityFramework\"";
        ConnectionStringSettings StringSettings = new ConnectionStringSettings("OWordpressContainer", conString,providerName);
        conSetting.ConnectionStrings.Remove(StringSettings);
        conSetting.ConnectionStrings.Add(StringSettings);
        Config.Save(ConfigurationSaveMode.Modified);

        return MessageFamily.Success.ToString();
    }
    catch(Exception ex)
    {
        return ex.Message.ToString();
    }
}

创建Connectionstring后,我保存了一些数据,但它仍然读取旧的连接字符串值:

public string SaveDbInfo(string userId, string password, string host, string dbName)
{
    try
    {
        var optionEntity = new Options();

        optionEntity.key = "DBInfo";
        optionEntity.value = "{userId:'" + userId + "',password:'" + password + "',host:'"+host+"',dbName:'"+dbName+"'}";

        objContext.Options.Add(optionEntity);
        objContext.SaveChanges();
        return MessageFamily.Success.ToString();
    }
    catch(Exception ex)
    {
        return ex.Message.ToString();

    }
}

2 个答案:

答案 0 :(得分:2)

我使用EntityConnectionStringBuilder

解决了这个问题
var ecsBuilder = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["OWordpressContainer"].ToString());

var sqlCsBuilder = new SqlConnectionStringBuilder(ecsBuilder.ProviderConnectionString)
{
    InitialCatalog = dbName,
    UserID=userId,
    DataSource=host,
    Password=password
};

var providerConnectionString = sqlCsBuilder.ToString();                
ecsBuilder.ProviderConnectionString = providerConnectionString;    
string contextConnectionString = ecsBuilder.ToString();   

using (var db = new DbContext(contextConnectionString))
{
    OWordpressContainer objContext = new OWordpressContainer(contextConnectionString);    
    var optionEntity = new Options();    
    optionEntity.key = "DBInfo";    
    optionEntity.value = "{userId:" + userId + "',password:'" + password + "',host:'" + host + "',dbName:'" + dbName + "'}";    
    objContext.Options.Add(optionEntity);    
    objContext.SaveChanges();
}

答案 1 :(得分:1)

更改连接字符串后,请尝试以下代码,并告诉我是否有帮助 -

ConfigurationManager.RefreshSection("connectionStrings");