修改配置文件

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace changeXml
{
    public class DbConfig
    {
        public string FileName { set; get; }
        private XmlDocument doc;

        public string[] dbs;

        public DbConfig(string fileName)
        {
            this.FileName = fileName;
            doc = new XmlDocument();
            doc.Load(fileName);
        }

        public DbConfig(string filename, string app, string prj, string[] dbs, string server, string database, string usr = "xx", string pwd = "xxx")
        {
            if (string.IsNullOrEmpty(filename))
                FileName = AppDomain.CurrentDomain.BaseDirectory + ".\\" + "Engine.xml";
            else
                FileName = filename;

            doc = new XmlDocument();
            doc.Load(FileName);

            //string app = "TEST2"; string prj = app; string db = "MAIN";
            foreach (var db in dbs)
            {
                string path = string.Format("/engine/all_prj_list/prj_list[@app_id='{0}']/prj[@id='{1}']/db_list/db[@id='{2}']", app, prj, db);
                SetServerInfo(doc, path, server, database, usr, pwd);

            }

            doc.Save(FileName);
        }


        private void SetServerInfo(XmlDocument doc, string path, string server, string database, string usr = "xx", string pwd = "xxx")
        {
            XmlNodeList list = doc.SelectNodes(path);
            foreach (XmlNode item in list)
            {
                SetServerInfo((XmlElement)item, server, database, usr, pwd);
            }
        }

        /// <summary>
        /// 设置连接信息
        /// </summary>
        /// <param name="ele"></param>
        /// <param name="server"></param>
        /// <param name="database"></param>
        /// <param name="usr"></param>
        /// <param name="pwd"></param>
        private void SetServerInfo(XmlElement ele, string server, string database, string usr = "xx", string pwd = "xxx")
        {
            DeleteAttrs(ele);

            ele.SetAttribute("server", server);
            ele.SetAttribute("catalog", database);
            ele.SetAttribute("drvtype", "0");
            ele.SetAttribute("dbtype", "1");
            ele.SetAttribute("driver", "SQLOLEDB.1");
            ele.SetAttribute("jdbc_driver", "");
            ele.SetAttribute("usr", usr);
            ele.SetAttribute("pwd", pwd);
            ele.SetAttribute("connstr", string.Format("Provider=SQLOLEDB.1;Password={0};Persist Security Info=True;User ID={1};Initial Catalog={2};Data Source={3}", pwd, usr, database, server));
            ele.SetAttribute("jdbc_url", "");
            ele.SetAttribute("pool_base", "0");
            ele.SetAttribute("pool_high", "0");
            ele.SetAttribute("shared", "False");
            ele.SetAttribute("single_connection", "False");

        }
        /// <summary>
        /// 删除非id/desc属性
        /// </summary>
        /// <param name="ele"></param>
        private void DeleteAttrs(XmlElement ele)
        {
            string[] attrs = { "id", "desc" };
            for (int i = ele.Attributes.Count - 1; i >= 0; i--)
            {
                if (!Contains(attrs, ele.Attributes[i].Name))
                    ele.Attributes.RemoveAt(i);
            }
        }


        private static bool Contains(string[] arr, string s)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].Equals(s))
                    return true;
            }
            return false;
        }
        public static string GetVarName(System.Linq.Expressions.Expression<Func<string, string>> exp)
        {
            return ((System.Linq.Expressions.MemberExpression)exp.Body).Member.Name;
            //            MyCommon.WriteLog(GetVarName(p => server));
        }


    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值