且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在web.config中动态设置连接字符串

更新时间:2023-11-07 16:33:22

使用NameSpace



Use NameSpace

using System.Configuration;
using System.Web.Configuration;

void ConfigurnewConnectionString(string server, string database, string userid, string password)
    {

        string str = "server=" + server + ";database=" + database + "; User ID=" + userid + "; Password=" + password + "";
        //Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
        //str = System.Web.Configuration.WebConfigurationManager.AppSettings["myKey"];
        //myConfiguration.Save();
        System.Configuration.Configuration Config1 = WebConfigurationManager.OpenWebConfiguration("~");
        ConnectionStringsSection conSetting = (ConnectionStringsSection)Config1.GetSection("connectionStrings");
        ConnectionStringSettings StringSettings = new ConnectionStringSettings("conn", "Data Source=" + server + ";Database=" + database + ";User ID=" + userid + ";Password=" + password + ";");
        conSetting.ConnectionStrings.Remove(StringSettings);
        conSetting.ConnectionStrings.Add(StringSettings);
        Config1.Save(ConfigurationSaveMode.Modified);
        //Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
        //myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text;
        //myConfiguration.Save();
    }





从此函数传递servername,databasename,userid,pasword



Pass servername,databasename,userid,pasword from this function


首先,你将需要在您的系统上设置身份验证(假设您还没有这样做),并让管理员登录。当管理员登录时,他们将能够访问您将编写的管理页面将提供更改密码的字段。然后,您的代码将验证数据库的密码 - 通常是通过尝试连接到它,然后您将值重新写回配置文件。



详细信息关于如何阅读配置文件(和保存)可以在这里 [ ^ ]。
First of all, you are going to need to set up authentication on your system (assuming you haven't done so already), and have the administrator sign in. When the administrator has signed in, they will be able to access an administration page that you will write which will provide a field for changing the password. Your code will then validate that the password against the database - typically by attempting to connect to it, and you will then write the value back into the config file.

Details on how to read the config file (and save) can be found here[^].


How to dynamically change connection string in web.config
Most of people wonder how they can dynamically change the contents of web.config file. To simplify things I have created couple of functions through which you can change the contents of web.config file. Here is the code to change the contents of web.config file:

/// <summary>
/// Updates the setting.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void UpdateSetting(string key, string value)
{
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    if (config.AppSettings.Settings[key] == null)
    {
        config.AppSettings.Settings.Add(key, value);
    }
    else
    {
        config.AppSettings.Settings[key].Value = value;
    }
    config.Save();
    ConfigurationManager.RefreshSection("appSettings");
}

/// <summary>
/// Updates the connection string.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void UpdateConnectionString(string key, string value)
{
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    if (config.ConnectionStrings.ConnectionStrings[key] == null)
    {
        config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(key, value));
    }
    else
    {
        config.ConnectionStrings.ConnectionStrings[key].ConnectionString = value;
    }
    config.Save();
    ConfigurationManager.RefreshSection("connectionStrings");
}



然后


then

protected void Page_Load(object sender, EventArgs e)
{
    UpdateSetting("test", "123");
    UpdateConnectionString("testcon", "12345");
}



http://zeeshanumardotnet.blogspot.in/2011/12/how-to-dynamically-change-connection.html [ ^ ]

[ ^ ]