使用ConfigurationManager保存时,提示:执行 connectionStrings 的配置节处理程序时出错。

wwh999 2009-09-29 06:59:36
随便在个Button的Click事件中贴入如下代码,运行即可看到效果:
  Dim strConnectionName As String = "LocalSQLExpress"
Dim setting As ConnectionStringSettings
setting = ConfigurationManager.ConnectionStrings(strConnectionName)
If setting Is Nothing Then
setting = New ConnectionStringSettings()
setting.Name = strConnectionName
setting.ConnectionString = "Data Source=.\SQLExpress;Initial Catalog=Northwind;Integrated Security=True;"

Dim config As Configuration
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming)
MessageBox.Show(config.FilePath)
Dim bb As New System.IO.FileInfo(config.FilePath)
Process.Start(bb.DirectoryName)

config.ConnectionStrings.ConnectionStrings.Add(setting)
config.Save(ConfigurationSaveMode.Modified)
End If

Using cn As New SqlConnection(setting.ConnectionString)
Try
cn.Open()
MessageBox.Show("Success!")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
...全文
889 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Z Steve 2011-01-30
  • 打赏
  • 举报
回复
真不够意思,都不写出怎么解决的!
wwh999 2009-10-01
  • 打赏
  • 举报
回复
问题我自己解决了.不过是个同步问题. 感觉没什么难度啊,我是不熟才这样.

看来还是没人认真帮忙解决问题..连愿读下代码的都没.

==>改变问题方向:
求:使用XmlDocument读/写App.config的已封好类(代码贴这也行,发到邮箱wwh2918@163.com也行.
搞完结贴..
ncjcz 2009-09-30
  • 打赏
  • 举报
回复
config.Save(ConfigurationSaveMode.Modified)

config.Save他会另生成一个config文件的
你用SaveAs
wwh999 2009-09-30
  • 打赏
  • 举报
回复
5楼的哥们..你的用Xml处理的方式我正在看...能帮我看看代码吗?
现在仅剩的问题是在程序运行时能改写OprConfig.vshost.exe.config,但一关掉之后,改写的内容就丢失了.
wwh999 2009-09-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ncjcz 的回复:]
config.Save(ConfigurationSaveMode.Modified)
config.Save他会另生成一个config文件的
你用SaveAs
[/Quote]

不行会报错的,你看一下我的演示错误的代码,看能不能改成有效果的(不长,应该写得蛮清楚的)
http://www.cbrj.net/download/OprConfig.rar
wjq 2009-09-30
  • 打赏
  • 举报
回复
private static string _appconfig = null;


/// <summary>
/// 获取或设置需要配置或读取的App.Config的文件名,若赋值为空(或未曾赋值)则下次读取该属性将取得调用方的EXE的默认config,否则返回上次设置的值。
/// </summary>
public static string AppConfig
{
get
{
if (_appconfig == null)
{
Type t = typeof(System.Configuration.ConfigurationManager).Assembly.GetType("System.Configuration.ClientConfigurationHost");
object cfghst = Activator.CreateInstance(t, true);
PropertyInfo pi = t.GetProperty("ConfigPaths", BindingFlags.Instance | BindingFlags.NonPublic);
object cfgpath = pi.GetValue(cfghst, null);

Type t1 = typeof(System.Configuration.ConfigurationManager).Assembly.GetType("System.Configuration.ClientConfigPaths");
pi = t1.GetProperty("ApplicationConfigUri", BindingFlags.Instance | BindingFlags.NonPublic);
string path = (string)pi.GetValue(cfgpath, null);

if (string.IsNullOrEmpty(path))
_appconfig = string.Empty;
else
_appconfig = path.Replace(".vshost.", ".");
}
return _appconfig;
}
set
{
_appconfig = value;
}
}
/// <summary>
/// 将明文的连接字符加密后,保存到App.config的相应小节的对应分行下
/// </summary>
/// <param name="Branch">BranchCode,如SH,HZ,GZ等</param>
/// <param name="connectionstring">连接串的明文</param>
public static void SaveConnection(string Branch, string connectionstring)
{
if (!File.Exists(AppConfig))
{
throw new DirectoryNotFoundException();
}
File.SetAttributes(Config.AppConfig, FileAttributes.Normal);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Config.AppConfig);
XmlNodeList xmllst = xmldoc.SelectNodes("/configuration/connectionStrings/add");
Branch = Branch.ToUpper();
if (xmldoc.SelectSingleNode("/configuration/connectionStrings") == null)
{
XmlNode n2 = xmldoc.CreateNode("element", "connectionStrings", "");
n2.InnerXml = "<add name=\"" + Branch + "\" connectionString=\"" + (connectionstring) + "\"/>";
xmldoc.SelectSingleNode("/configuration").AppendChild(n2);
xmldoc.Save(AppConfig);
}
else if (xmllst.Count == 0)
{
XmlNode n2 = xmldoc.CreateNode("element", "add", "");
XmlAttribute xa = xmldoc.CreateAttribute("name");
xa.Value = Branch;
n2.Attributes.Append(xa);
xa = xmldoc.CreateAttribute("connectionString");
xa.Value = (connectionstring);
n2.Attributes.Append(xa);
xmldoc.SelectSingleNode("/configuration/connectionStrings").AppendChild(n2);
xmldoc.Save(AppConfig);
}
else
{
bool existed = false;
foreach (XmlNode n1 in xmllst)
{
if (n1.Attributes["name"].Value.ToUpper() == Branch)
{
n1.Attributes["connectionString"].Value = (connectionstring);
xmldoc.Save(AppConfig);
existed = true;
break;
}
}
if (!existed)
{
XmlNode xmlnd = xmldoc.SelectSingleNode("/configuration/connectionStrings");
XmlNode n2 = xmldoc.CreateNode("element", "add", "");
XmlAttribute xa = xmldoc.CreateAttribute("name");
xa.Value = Branch;
n2.Attributes.Append(xa);
xa = xmldoc.CreateAttribute("connectionString");
xa.Value = NCS.Tools.QuickEncrypt.EncryptConfig(connectionstring);
n2.Attributes.Append(xa);
xmlnd.AppendChild(n2);
xmldoc.Save(Config.AppConfig);
}
}
ConfigurationManager.RefreshSection("connectionStrings");
}
wwh999 2009-09-29
  • 打赏
  • 举报
回复
使用楼上的代码,是不会报错,但是,所有的修改都不能成功(无论查看App.Config还是..exe.cofig,)都没有实际更改.

整了一整天了..汗.楼上的高手,有没有做好了的类,或者能看到效果的示例给一个?崩溃了....
我发的另个贴也是一码事:http://topic.csdn.net/u/20090929/18/9eba3895-32ab-4586-8986-f2e52984a1bf.html

Mail:wwh2918@163.com
谢谢.
wuyq11 2009-09-29
  • 打赏
  • 举报
回复
看看修改后的文件,配置不正确
Public Sub SaveConfig(ByVal configKey As String, ByVal configValue As String)
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.AppSettings.Settings.Clear()
config.AppSettings.Settings.Add(configKey, configValue)
config.Save()
End Sub

Public Sub SaveConfig(ByVal configKey As String, ByVal configValue As String)
Dim doc As New XmlDocument()
doc.Load(strFileName)
Dim nodes As XmlNodeList = doc.GetElementsByTagName("add")
For Each node As XmlNode In nodes
Dim xe As XmlElement = DirectCast(node, XmlElement)
If xe.GetAttribute("key") = configKey Then
xe.SetAttribute("value", configValue)
End If
Next
doc.Save(strFileName)
End Sub

Public Sub SaveConfig(ByVal configKey As String, ByVal configValue As String)
For i As Integer = 0 To nodes.Count - 1
Dim att As XmlAttribute = nodes(i).Attributes("key")
If att.Value = "" & configKey & "" Then
att = nodes(i).Attributes("value")
att.Value = configValue
Exit For
End If
Next
doc.Save(strFileName)
End Sub
wwh999 2009-09-29
  • 打赏
  • 举报
回复
在执行到Save时,提示
执行 connectionStrings 的配置节处理程序时出错。请问如何解决?谢谢

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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