using Pms.UserEvent.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace CasunModelDB
{
    public class CDBSettingData
    {
        public string DataSource = ".";
        public string InitialCatalog = "CasunQRCodeDB2";
        public string uid = "sa";
        public string pwd = "123";

        //public string KWDataSource = ".";
        //public string KWInitialCatalog = "CasunQRCodeDB2";
        //public string KWuid = "sa";
        //public string KWpwd = "123";
        //public int UpdateTime = 1;
    }

    public class CDBSetting
    {
        public static CDBSettingData g_DBSetting = new CDBSettingData();
        public static string g_ConnectionString = "Data Source=.;Initial Catalog=ss;Integrated Security=True";
        public static string kwg_ConnectionString = "Data Source=.;Initial Catalog=ss;Integrated Security=True";
        public static void InitDBSetting()
        {
            if (!File.Exists(Global.CDBpath))
            {
                File.Create(Global.CDBpath);
            }
            else
            {
                //反序列化
                XmlSerializer serializer = new XmlSerializer(CDBSetting.g_DBSetting.GetType());
                try
                {
                    using (TextReader tr = new StreamReader(Global.CDBpath))
                    {
                        CDBSetting.g_DBSetting = (CDBSettingData)serializer.Deserialize(tr);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                g_ConnectionString = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=false;uid={2};pwd={3}"
                 , g_DBSetting.DataSource
                 , g_DBSetting.InitialCatalog
                 , g_DBSetting.uid
                 , g_DBSetting.pwd
                 );

            }
        }
    }
}
