WebConfig操作帮助类

  ///
/// ConfigurationOperator 的摘要说明
///
public class ConfigurationOperator : IDisposable
{
private Configuration config;
public ConfigurationOperator()
: this(HttpContext.Current.Request.ApplicationPath)
{ }
public ConfigurationOperator(string path)
{
// Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
config = WebConfigurationManager.OpenWebConfiguration(path);
}
///
/// 设置应用程序配置节点,如果已经存在此节点,则会修改该节点的值,否则添加此节点
///
/// 节点名称
/// 节点值
public void SetAppSetting(string key, string value)
{
AppSettingsSection appSetting = (AppSettingsSection)config.GetSection("appSettings");
if (appSetting.Settings[key] == null)//如果不存在此节点,则添加
{
appSetting.Settings.Add(key, value);
}
else//如果存在此节点,则修改
{
appSetting.Settings[key].Value = value;
}
}
///
/// 设置数据库连接字符串节点,如果不存在此节点,则会添加此节点及对应的值,存在则修改
///
/// 节点名称
/// 节点值
public void SetConnectionString(string key, string connectionString)
{
ConnectionStringsSection connectionSetting = (ConnectionStringsSection)config.GetSection("connectionStrings");
if (connectionSetting.ConnectionStrings[key] == null)//如果不存在此节点,则添加
{
ConnectionStringSettings connectionStringSettings = new ConnectionStringSettings(key, connectionString);
connectionSetting.ConnectionStrings.Add(connectionStringSettings);
}
else//如果存在此节点,则修改
{
connectionSetting.ConnectionStrings[key].ConnectionString = connectionString;
}
}
///
/// 保存所作的修改
///
public void Save()
{
config.Save();
config = null;
}
public void Dispose()
{
if (config != null)
{
config.Save();
}
} /// <summary>
/// 根据数据库连接串,测试是否连接成功
/// </summary>
/// <param name="connText"></param>
/// <returns></returns>
public string ResultText(string connText)
{
SqlConnection conn = new SqlConnection();
string txtValue = string.Empty; using (SqlConnection connection = new SqlConnection(connText))
{
try
{
connection.Open();
}
catch (Exception ex)
{
txtValue = ex.ToString();
}
return txtValue;
}
}
}

Web窗体.CS后台调用方法

        /// <summary>
/// 数据库连接串更新
/// </summary>
/// <param name="name">数据库连接串名称</param>
/// <param name="connStr">数据库连接(加密)</param>
public static void UpdateConnectionStrings(string name,string connStr)
{
//调用
ConfigurationOperator cfgOper = new ConfigurationOperator();
cfgOper.SetConnectionString(name, connStr);//添加一个节点,如果有就修改 cfgOper.Save();
cfgOper.Dispose();
} /// <summary>
/// 参数更新
/// </summary>
/// <param name="key">参数名称</param>
/// <param name="value">参数值</param>
public static void UpdateAppSettings(string key,string value)
{
//调用
ConfigurationOperator cfgOper = new ConfigurationOperator();
cfgOper.SetAppSetting(key, value);//添加一个节点,如果有就修改 cfgOper.Save();
cfgOper.Dispose();
}

参数获取

ConfigurationManager.AppSettings["canshu1"]

数据库链接获取

ConfigurationManager.ConnectionStrings["conStr"].ConnectionString

其他:

var config=  ConfigurationManager.AppSettings["minAmount"];//获取值
Configuration cfa = WebConfigurationManager.OpenWebConfiguration("~");
cfa.AppSettings.Settings.Add("key", "value");//添加值
cfa.AppSettings.Settings["redRule"].Value = "-1";//修改值
cfa.Save();//保存
//ConfigurationManager.RefreshSection("appSettings"); //强制刷新配置文件

BS Web窗体 动态修改WebConfig文件参数及数据库链接串的更多相关文章

  1. [置顶] c# asp.net 修改webconfig文件 配置

    c# asp.net 修改webconfig文件 配置 #region 修改config文件 /// <summary> /// 修改config文件(AppSetting节点) /// ...

  2. 运行时动态修改webconfig

    网站运行时,如果直接手动修改webconfig.xml,会造成线程池重启,对于PV大的网站来说风险是很大的,并且如果有多台服务器,逐台修改也会造成数据不一致. .NET 2.0后可以通过代码修改web ...

  3. oracle通过修改控制文件scn推进数据库scn

    数据库当前scn 代码如下 复制代码 idle> select checkpoint_change# from v$database; CHECKPOINT_CHANGE#----------- ...

  4. maven 根据P参数值打包动态修改properties文件中值或一定properties

    需求:由于最近开发clover项目 ,没有使用spring,更没有使用任何框架,而使用J2EE的web工程,所以连接ZK和MongoDB.Redis等服务器需用指定properties文件, 而目前公 ...

  5. jmeter动态修改线程组参数

    jmeter非gui模式修改线程属性进行性能测试 在使用JMeter进行性能测试自动化时,如果按照平常的非Gui模式就是脚本每次修改参数都需要在gui模式下修改保存之后,然后在非gui模式之后运行,这 ...

  6. c# asp.net 修改webconfig文件 配置

    #region 修改config文件 /// <summary> /// 修改config文件(AppSetting节点) /// </summary> /// <par ...

  7. 动态修改PE文件图标(使用UpdateResource API函数)

    PE文件的图标存储在资源文件中,而操作资源要用到的API函数就是UpdateResource首先我们需要先了解一下ICO格式,参考资料:http://www.moon-soft.com/program ...

  8. 动态修改css文件中,具体的class中的个别属性值。

    function setStyleSheetObjCssClassProperty(pStyleSheetObj, pSelectorText, pProperty, pValue) { var pS ...

  9. 使用C#创建winform窗体,修改debugwen文件夹下exe应用程序的默认图标

    在做一个接口程序是遇到的问题,记录一下: 在解决方案资源管理器上,右击项目名称——属性——点击图标和清单右边的的按纽——去Debug文件夹中找到自己的图标,打开.然后保存.

随机推荐

  1. pytest文档8-html报告报错截图+失败重跑

    前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...

  2. sqlserver锁大全

    锁定提示                                 描述  HOLDLOCK        将共享锁保留到事务完成,而不是在相应的表.行或数据页不再需要时就立即释放锁.HOLDL ...

  3. BigDecimal类型数据保留两位小数即百分比运算

    方法示例: DecimalFormat df = new DecimalFormat("0.00"); Object price = 2; Object price1 = 2.3; ...

  4. Thymeleaf th:action

    th:attr 任何属性值 <form action="subscribe.html" th:attr="action=@{/subscribe}"> ...

  5. MANIFEST.MF文件Class-Path:节点需要引入的jar太多解决方案

    每行开头结尾都要有一个空格(半角的) 例子如下: Manifest-Version: 1.0 Class-Path: lib/mongo-java-driver-2.11.4.jar lib/guav ...

  6. iTextSharp 合并PDF后,无法删除已经合并的单个文件

    private void MergePDFFiles(string[] fileList, string outMergeFile) { List<PdfReader> readerLis ...

  7. GPGPU OpenCL使用结构体数据

    OpenCL编程中可以使用结构体,只需要在核函数kernel中提供同样的结构体申明就可以啦. 如果在主函数中定义了结构体: typedef struct studentNode{ int age; f ...

  8. vnstat 查看服务器带宽统计命令

    vnStat是一个Linux下的网络流量监控软件,它记录指定网卡每日的传输流量日志. 它并非基于网络包的过滤,而是分析文件系统- /proc, 所以vnStat无需root的权限就可使用. ,它还自带 ...

  9. Ext.encode 与 Ext.decode .

    Ext.encode( Mixed o ) : String: json对象转换json字符串 Ext.decode( String json ) : Object: json字符串转换json对象 ...

  10. Drag & drop a button widget

    In the following example, we will demonstrate how to drag & drop a button widget. #!/usr/bin/pyt ...