zoukankan      html  css  js  c++  java
  • 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文件出错,请重试!"); }
  • 相关阅读:
    第三章 操作符
    exit函数
    详解C++ friend关键字
    放假了,暂告一段落,迎接研究生
    使用const 提高函数的健壮性
    使用断言assert
    对return 语句的正确性和效率进行检查
    函数堆栈
    somethings about QSplitter
    引用和引用参数
  • 原文地址:https://www.cnblogs.com/hrx521/p/1055245.html
Copyright © 2011-2022 走看看