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));
}
}
}