在web.config中动态设置连接字符串 [英] dynamically set connection string in web.config

查看:67
本文介绍了在web.config中动态设置连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

假设我的web.config中有这个连接字符串(Asp.net C#)



Hi All,
Suppose I have this connection string in my web.config(Asp.net C#)

<appSettings>
<add key="constring" value="server=Virus-WS05;user id=sa;database=Virus; password=Virus2011"/>
</appSettings>





现在这里如果admin将数据库的密码从virus2011更改为病毒,那怎么能admin为所有用户动态设置web.config。

(不是从ftp下载web.config然后再次托管它,因为这个过程很糟糕).sdf



谢谢



添加了代码块,禁用了忽略HTML ...选项 - OriginalGriff [/ edit]



Now here If admin change the password of the database from virus2011 to virus, then how can admin set it in web.config dynamically for all the users.
(not by downloading the web.config from ftp and then again hosting it as this process sucks).sdf

Thanks

[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]

推荐答案

使用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 [ ^ ]

[ ^ ]


这篇关于在web.config中动态设置连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆