数据库配置文件(config.xml)
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ConnectionString" value="server=192.168.100.116;database=BDCY;uid=sa;pwd=123456;" /> <add key="ConnString" value="server=(local);database=gxjd;uid=sa;pwd=" /> <add key="BakData" value="W1" /> <add key="dogServer" value="192.168.100.116"> </add> <add key="dogport" value="9898"> </add> </appSettings> </configuration>
引用命名空间(using System.Xml;)
读取XML中的数据库连接字符串
/// <summary> /// 读取XML中的数据数据库连接字符串 /// </summary> /// <param name="value"></param> /// <param name="key"></param> /// <returns></returns> private string ConfigGetValues(string key) { string strRes = string.Empty; XmlDocument xDoc = new XmlDocument(); xDoc.Load("Config.xml"); XmlNode xNode; XmlElement xElement; xNode = xDoc.SelectSingleNode("//appSettings"); xElement = (XmlElement)xNode.SelectSingleNode("add[@key='"+key+"']"); if (xElement != null) { strRes = xElement.GetAttribute("value"); } return strRes; }
写入XML中的数据库连接字符串
/// <summary> /// 写入XML中的数据 /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> /// <returns></returns> private void ConfigSetValues(string key,string value) { XmlDocument xDoc = new XmlDocument(); xDoc.Load("Config.xml"); XmlNode xNode; XmlElement xElement; XmlElement xElement1; xNode = xDoc.SelectSingleNode("//appSettings"); xElement = (XmlElement)xNode.SelectSingleNode("//add[@key='"+key+"']"); if (xElement != null) { xElement.SetAttribute("value", value); //"value"固定的AppStrings结点属性 } else { xElement1 = xDoc.CreateElement("add"); xElement1.SetAttribute("key", key); //"key"固定的AppStrings结点属性 xElement1.SetAttribute("value", value); //"value"固定的AppStrings结点属性 xNode.AppendChild(xElement1); } xDoc.Save("Config.xml"); }
完整代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; namespace Config { public partial class XMLCheck : Form { public XMLCheck() { InitializeComponent(); } //读取数据 private void btnRead_Click(object sender, EventArgs e) { GetConfigSub("ConnectionString"); } //写入数据 private void btnWrite_Click(object sender, EventArgs e) { SetConfigSub(); MessageBox.Show("修改成功!"); } /// <summary> /// 读取XML中的数据数据库连接字符串 /// </summary> /// <param name="value"></param> /// <param name="key"></param> /// <returns></returns> private string ConfigGetValues(string key) { string strRes = string.Empty; XmlDocument xDoc = new XmlDocument(); xDoc.Load("Config.xml"); XmlNode xNode; XmlElement xElement; xNode = xDoc.SelectSingleNode("//appSettings"); xElement = (XmlElement)xNode.SelectSingleNode("add[@key='"+key+"']"); if (xElement != null) { strRes = xElement.GetAttribute("value"); } return strRes; } /// <summary> /// 获取数据库连接的子信息 /// </summary> private void GetConfigSub(string key) { string strRes = ConfigGetValues(key); string[] arr = strRes.Split(';'); txbServerName.Text =arr[0].Substring(arr[0].IndexOf('=')+1); txbDBName.Text = arr[1].Substring(arr[1].IndexOf('=') + 1); txbAccountName.Text = arr[2].Substring(arr[2].IndexOf('=') + 1); txbDBPwd.Text = arr[3].Substring(arr[3].IndexOf('=') + 1); txbDogAddress.Text = ConfigGetValues("dogServer"); } /// <summary> /// 写入XML中的数据 /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> /// <returns></returns> private void ConfigSetValues(string key,string value) { XmlDocument xDoc = new XmlDocument(); xDoc.Load("Config.xml"); XmlNode xNode; XmlElement xElement; XmlElement xElement1; xNode = xDoc.SelectSingleNode("//appSettings"); xElement = (XmlElement)xNode.SelectSingleNode("//add[@key='"+key+"']"); if (xElement != null) { xElement.SetAttribute("value", value); //"value"固定的AppStrings结点属性 } else { xElement1 = xDoc.CreateElement("add"); xElement1.SetAttribute("key", key); //"key"固定的AppStrings结点属性 xElement1.SetAttribute("value", value); //"value"固定的AppStrings结点属性 xNode.AppendChild(xElement1); } xDoc.Save("Config.xml"); } /// <summary> /// 设置数据库连接字符串 /// </summary> private void SetConfigSub() { string serverName = "server=" + txbServerName.Text + ";"; string dbName = "database=" + txbDBName.Text + ";"; string dbAccount = "uid=" + txbAccountName.Text + ";"; string dbPwd = "pwd=" + txbDBPwd.Text + ";"; string dogServer = txbDogAddress.Text; string appSetting = serverName + dbName + dbAccount + dbPwd; ConfigSetValues("ConnectionString", appSetting); ConfigSetValues("dogServer", dogServer); } } }
效果图如下: