C#动态连接数据库如何实现? 我用VS2008做的是C#windows窗体的应用程序,想实现程序在生成可执行文件之后,有这么一个功能可以动态配置数据库连接的参数,程序在安装之后更灵活的配置数据库连接参数。是做个可以动态修改App.config文件的窗体吗,还有什么思路,有可以参考的代码吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 动态加载或者动态修改app.config都可以的。程序修改vapp.config文件的方法: public static void SetKeyValue(string AppKey, string AppValue) { XmlDocument xDoc = new XmlDocument(); xDoc.Load(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "app.config"); XmlNode xNode; XmlElement xElem1;//XmlElement xElem2; xNode = xDoc.SelectSingleNode("//appSettings"); xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']"); if (xElem1 != null) { xElem1.SetAttribute("value", AppValue); } /* else { xElem2 = xDoc.CreateElement("add"); xElem2.SetAttribute("name", AppKey); xElem2.SetAttribute("connectionString", AppValue); xNode.AppendChild(xElem2); }*/ xDoc.Save(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "app.config"); } C#程序打包 用代码构建的ListView如何实现一个事件? 判断当前combobox的输入是否在绑定的数据中出现 怎么用C#控制IIS的启动等操作 C#中能绘制3D图形动画吗?请高手支招~ 存储过程 DataTable对象的dt.Clear()和dt.rows.Clear()有什么区别? backgroundworker TargetInvocationException 测试测试,顺便提点意见.... 关于用c#定时操作数据库的问题 文本框联动 倒三角形 怎样补充代码
public static void SetKeyValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument(); xDoc.Load(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "app.config"); XmlNode xNode; XmlElement xElem1;//XmlElement xElem2; xNode = xDoc.SelectSingleNode("//appSettings"); xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']"); if (xElem1 != null)
{
xElem1.SetAttribute("value", AppValue);
}
/* else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("name", AppKey);
xElem2.SetAttribute("connectionString", AppValue);
xNode.AppendChild(xElem2);
}*/
xDoc.Save(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "app.config");
}