C#2005用XML来保存连接数据库的字符串(这样改变了服务器连接配制只需改一下这个文件就成了),琢磨了几天终于有解了,和大家分享一下,希望高手指点!

在BIN目录下建一个名为app.config的XML文件,内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>Data Source=NIU;Initial Catalog=glass_test;Persist Security Info=True;User ID=sa;Password=2008</connectionStrings>
</configuration>



//////////////////////////////////////////下面是程序中获取连接字串到变量ConStr的方法///////////////////////////
//读取app.config里的连接数据库的字串
//加引用空间
using System.Xml;
//主要代码 
    string ConStr=null;    
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("app.config");
                XmlNodeList elemlist = doc.GetElementsByTagName("connectionStrings");
                ConStr = elemlist[0].InnerXml;
            }
            catch { MessageBox.Show("读取XML文件出错,请重试!"); }
原文地址:https://www.cnblogs.com/hrx521/p/1055245.html