读写web.config的简单问题?

yumanqing 2008-08-08 10:50:53
在程序中有一段代码是这样的:
创建数据库->将新数据库的连接字符串写入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];


...全文
175 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yumanqing 2008-08-08
  • 打赏
  • 举报
回复
谢谢,先在想想吧其它办法,等待...
linekery 2008-08-08
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 andy_1027 的回复:]
配置文件必须是程序重启后才会加载,你如果急着读出来还是不要用WEB。CONFIG,用其它配置文件好了。
[/Quote]

不用重启

自动重新加载

只要让程序空闲出来 - - 问题是 一个事件都没执行完 程序当然不重新加载啊
你等事件完了 之后在读取 得了呗。。。
andy_1027 2008-08-08
  • 打赏
  • 举报
回复
配置文件必须是程序重启后才会加载,你如果急着读出来还是不要用WEB。CONFIG,用其它配置文件好了。
zzlazio 2008-08-08
  • 打赏
  • 举报
回复
晕,上面代码顺手写的,忘记返回了connectionString 了
zzlazio 2008-08-08
  • 打赏
  • 举报
回复
写和读在同一事件里的话,个人认为在读的时候,重新先读一边配置文件比较合适


private void ReadConfig()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection sc = (AppSettingsSection)config.GetSection("appSettings");
string connectionString = sc.Settings[appKeys[keyNameLocal]].Value
}
windboyzsj 2008-08-08
  • 打赏
  • 举报
回复

System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(HttpContext.Current.Server.MapPath("web.config"));
config.AppSettings.Settings.Add("strConFor", "Persist Security Info=false;InitialCatalog=Sales;User Id=sa;Password=;server=127.0.0.1");
config.Save(ConfigurationSaveMode.Modified);
//读取
string keyNameLocal = "strConFor";
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
string conn = System.Configuration.ConfigurationManager.AppSettings[keyNameLocal];


或者你换个方法,来添加。
yumanqing 2008-08-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 windboyzsj 的回复:]
再次搜索之前重载一下

...
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
string conn = System.Configuration.ConfigurationManager.AppSettings[keyNameLocal];
...
[/Quote]

也不行,继续等待中,不解决晚上奥运会看的就不安心呀,帮忙一下。。。。。
格拉 2008-08-08
  • 打赏
  • 举报
回复
mark
linekery 2008-08-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 windboyzsj 的回复:]
再次搜索之前重载一下

...
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
string conn = System.Configuration.ConfigurationManager.AppSettings[keyNameLocal];
...
[/Quote]


主动重载 不会结束 当前会话么? 那样所有权限 之类的就都没了
linekery 2008-08-08
  • 打赏
  • 举报
回复
修改了 重新加载 得你 当前干的事情干完了才行呀 - - 程序才会主动重新加载
如果有验证的话 会自动回默认页的 - - 在一个事件里 也不能强迫立刻重新加载的
你应该创建数据库 作为单独事件 执行完毕之后 会自动加载的 以后就能查到了
windboyzsj 2008-08-08
  • 打赏
  • 举报
回复
再次搜索之前重载一下

...
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
string conn = System.Configuration.ConfigurationManager.AppSettings[keyNameLocal];
...
yumanqing 2008-08-08
  • 打赏
  • 举报
回复
我是想用配置文件方便一些,那在运行过程中修改了配置文件怎么能及时的反映出来呢?
yumanqing 2008-08-08
  • 打赏
  • 举报
回复
我又试验了,用了两个按钮,写 和 读,没有问题,可先写后读放到一个事件就有问题,不知道具体什么原因,这么解决一下,
感谢!
maotin 2008-08-08
  • 打赏
  • 举报
回复
配置文件,在程序启动时加载,可能你修改后,程序不会自动重新加载配置文件!
实在不行,就直接读取文件,不要读配置文件了,呵呵!
linekery 2008-08-08
  • 打赏
  • 举报
回复
更改后 程序会自动重新加载 config文件 同时 会话自动结束
可能是还没加载呢 - - 你就读取了 猜测

你可以 写 和 读 分开做 用2个事件看看

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧