C#从XML文件读取连接数据库语句

新建一个XML文件,文件内容:

<Set>
  <IsEditSfz>1</IsEditSfz>
  <Server>192.168.1.13</Server>
  <Database>hrpaobu</Database>
  <UserID>sa</UserID>
  <Pws />
</Set>

里面有要连接是数据库的IP,数据库的名称,登录用户和密码;

 C#程序里面的程序语句:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;

namespace HrPaoBu
{
    /// <summary>
    /// 主函数
    /// </summary>
    static class ClsMain
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>

        public static string varServer = "";//= "dev";
        public static string varDatabase = "";// = "book";
        public static string varUserId = "";//= "sa";
        public static string varPws = "";//= "sa";

        public static string varLoginNo = "";//application login no
        public static string varLoginName = "";
        public static bool varIsEditSfz = false;

        //public static List<string> lstOtherLogin = new List<string>();

        public static frmMain sfrmMain = null;

        private static string strConn;
        /// <summary>
        /// 连接数据库
        /// </summary>
        public static System.Data.SqlClient.SqlConnection conn
        {
            get
            {
                return new System.Data.SqlClient.SqlConnection(strConn);
            }
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ReportUtility.RegisterGridppReport();
            try
            {
                InitConnectionStr();
            }
            catch (Exception e)
            {
                MessageBox.Show("初始化链接出错,可能SET.XML配置文件有误或不存在!!\n" + e.Message);
                Application.Exit();
            }

            Application.Run(new frmMain  ()); //
            if (sfrmMain != null)
                Application.Run(sfrmMain);
            //Application.Run(new frmProductValue());
            //sfrmMain = new frmMain();
            //Application.Run(sfrmMain);
        }


        public static void InitConnectionStr()
        {
            XmlTextReader xmlSet;
            string[] s = new string[5];
            int i = 0;
            //XmlDocument xmlDoc=new XmlDocument();
            try
            {
                xmlSet = new XmlTextReader("SET.xml");
                while (xmlSet.Read())
                {
                    if (xmlSet.NodeType == XmlNodeType.Text)
                    {
                        s[i] = xmlSet.Value.ToString();
                        i++;
                    }
                }
                xmlSet.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                xmlSet = null;
            }
            varIsEditSfz = s[0] == "0" ? false : true;
            varServer = s[1];
            varDatabase = s[2];
            varUserId = s[3];
            varPws = s[4];

            strConn = "user id=" + varUserId +
                ";password=" + varPws +
                ";data source=" + varServer +
                ";Pooling = true" +   //
                ";Min Pool Size = 0" +  //
                ";Max Pool Size = 5" +  //
                ";persist security info=False;initial catalog=" + varDatabase;
        }

    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值