读写web.config的简单问题?
创建数据库->将新数据库的连接字符串写入web.config->读取连接字符串->向新数据库插入一行数据
现在代码都写好了,现在的问题是写入web.config也已经写入了,可是读取
string keyNameLocal = "strConFor" + CoNo.ToString() + "Local";
connectionString = System.Configuration.ConfigurationManager.AppSettings[keyNameLocal];
的时候总是提示是null,停止调试,打开web.config查看,的确是写入了,我感觉好像当时写在内存中,读取的好像不是最新的数据
可是 webc.config已经保存了呀;郁闷...
关键代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.Reflection;
public enum ConfigFileType
{
WebConfig,
AppConfig
}
namespace SalesServer.Operate
{
/// <summary>
/// web.config操作类
/// </summary>
public class ReadWriteConfig
{
public string docName = String.Empty;
private XmlNode node = null;
private int _configType;
public string message;
public int ConfigType
{
get { return _configType; }
set { _configType = value; }
}
#region 读取
public string GetValue(string key)
{ //读取
try
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new InvalidOperationException("appSettings section not found");
}
// XPath select setting "add" element that contains this key to remove
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (addElem == null)
{
message = "此key不存在!";
return message;
}
message = System.Configuration.ConfigurationManager.AppSettings[key];
return message;
}
catch
{
message = "操作异常,获取value值失败!";
return message;
}
}
#endregion
#region 增加
public string SetValue(string key, string value)
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new InvalidOperationException("appSettings section not found");
}
try
{
// XPath select setting "add" element that contains this key
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (addElem != null)
{
message = "此key已经存在!";
return message;
}
// not found, so we need to add the element, key and value
else
{
XmlElement entry = cfgDoc.CreateElement("add");
entry.SetAttribute("key", key);
entry.SetAttribute("value", value);
node.AppendChild(entry);
}
//save it
saveConfigDoc(cfgDoc, docName);
message = "添加成功!";
return message;
}
catch
{
message = "操作异常,添加失败!";
return message;
}
}
#endregion
#region 保存web.config
private void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath)
{
try
{
XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null);
writer.Formatting = Formatting.Indented;
cfgDoc.WriteTo(writer);
writer.Flush();
writer.Close();
return;
}
catch
{
throw;
}
}
#endregion
#region 删除
public string removeElement(string elementKey)
{ // 删除
try
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new InvalidOperationException("appSettings section not found");
}
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + elementKey + "']");
if (addElem == null)
{
message = "此key不存在!";
return message;
}
// XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
saveConfigDoc(cfgDoc, docName);
message = "删除成功!";
return message;
}
catch
{
message = "操作异常,删除失败!";
return message;
}
}
#endregion
#region 修改
public string modifyElement(string elementKey, string elementValue)
{ //修改
try
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new InvalidOperationException("appSettings section not found");
}
// XPath select setting "add" element that contains this key to remove
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + elementKey + "']");
if (addElem == null)
{
message = "此key不存在!";
return message;
}
addElem.SetAttribute("value", elementValue);
//save it
saveConfigDoc(cfgDoc, docName);
message = "修改成功!";
return message;
}
catch
{
message = "操作异常,修改失败!";
return message;
}
}
#endregion
#region 加载
private XmlDocument loadConfigDoc(XmlDocument cfgDoc)
{
// load the config file
if (Convert.ToInt32(ConfigType) == Convert.ToInt32(ConfigFileType.AppConfig))
{
docName = ((Assembly.GetEntryAssembly()).GetName()).Name;
docName += ".exe.config";
}
else
{
docName = HttpContext.Current.Server.MapPath("web.config");
}
cfgDoc.Load(docName);
return cfgDoc;
}
#endregion
}
}
//写入
ReadWriteConfig config = new ReadWriteConfig();
config.ConfigType = (int)ConfigFileType.WebConfig;
config.SetValue("strConFor" + CoNo.ToString() + "Local", "Persist Security Info=false;InitialCatalog=Sales";User Id=sa;Password=;server=127.0.0.1");
//读取
string keyNameLocal=("strConFor" + CoNo.ToString() + "Local",;
System.Configuration.ConfigurationManager.AppSettings[keyNameLocal];
相关推荐
xmlbean轻轻松松读写XML的利器
转《利用XMLBean轻轻松松读写XML》 XML在Java应用程序里变得越来越重要, 广泛应用于数据存储和交换. 比如我们常见的配置文件,都是以XML方式存储的. XML还应用于Java Message Service和Web Services等技术作为数据交换. 因此,正确读写XML文档是XML应用的基础. Java提供了SAX和DOM两种方式用于解析XML,但即便如此,要读写一个稍微复杂的XML,也不是一件容易的事. 二、XMLBean简介 Hibernate已经成为目前流行的面向Java环境的对象/关系数据库映射工具. 在Hibernate等对象/关系数据库映射工具出现之前,对数据库的操作是通过JDBC来实现的,对数据库的任何操作,开发人员都要自己写SQL语句来实现. 对象/关系数据库映射工具出现后,对数据库的操作转成对JavaBean的操作,极大方便了数据库开发. 所以如果有一个类似的工具能够实现将对XML的读写转成对JavaBean的操作,将会简化XML的读写,即使对XML不熟悉的开发人员也能方便地读写XML. 这个工具就是XMLBean.
c#配置文件读写.rar
c#配置文件读写.rar
java项目乱码问题
java项目乱码问题的处理方法,非常详细的
最新ckeditor_ckfinder整合超完整版 for PHP 附ckeditor完整中文配置
最新ckeditor_ckfinder整合超完整版 for PHP版让你的文本编辑器支持图片和FLASH的上传 附ckeditor完整中文配置
Nginx下配置codeigniter框架方法
由于项目迁移,不得不在Nginx下配置codeigniter框架,开始怎么配置都不成功,经过度娘以及众网友的热心帮忙,终于完成了正确的设置,这里分享给大家,有需要的小伙伴参考下


