在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文件出错,请重试!"); }
posted on 2008-01-27 21:34  hrx521  阅读(1201)  评论(7编辑  收藏  举报