WinForm中如何获取连接字符串,并保存修改。
WinForm中如何获取连接字符串,并保存修改。
加载时读取出保存的连接字符串,显示到文本框中。
点击测试连接按钮时,使用输入的连接字符串进行服务器连接尝试,弹出尝试结果信息。
点击确定按钮时将输入的连接字符串保存
[解决办法]
- C# code
System.Xml.XmlDocument domSysinfo=new System.Xml.XmlDocument(); System.Xml.XmlNode root; System.Xml.XmlNode node; System.Xml.XmlNodeList nodeList; domSysinfo.Load(AppDomain.CurrentDomain.BaseDirectory + "\\app.config"); root=domSysinfo.SelectSingleNode("//configuration"); node=root.SelectSingleNode("//appSettings"); nodeList=node.SelectNodes("add"); for(int i=0;i<nodeList.Count;i++) { if(nodeList[i].Attributes["key"].InnerText.Equals("ConnStr"))// { nodeList[i].Attributes["value"].InnerText=""; } } domSysinfo.Save(AppDomain.CurrentDomain.BaseDirectory + "\\app.config");
[解决办法]
你的WindowsApplication项目下有个Properties文件夹,下面有个Settings.settings文件,双击进去,新建一个值,类型选择连接字符串
读的时候:
string connString = Properties.Settings.Default.ConnString;
写的时候
Properties.Settings.Default.ConnString=“”;
Properties.Settings.Defaul.Save();
[解决办法]
9楼的方法很简单,优先考虑