Exemplo n.º 1
0
        /// <summary>
        /// 从配置文件MyDB.xml加载连接参数
        /// </summary>
        /// <returns></returns>
        public bool LoadSettings()
        {
            bool bSuccessLoaded = false;

            try
            {
                string DbConfigFile = Directory.GetCurrentDirectory() + "\\" + DB_CONFIG_FILE;
                try
                {
                    ServerType = (DbServerType)Enum.Parse(typeof(DbServerType), MyXml.XmlRead(DbConfigFile, "DbConfig", "ServerType", DbServerType.SqlServer.ToString()), true);
                }
                catch
                {
                    ServerType = DbServerType.SqlServer;
                }
                Server = MyXml.XmlRead(DbConfigFile, "DbConfig", "Server", "localhost\\sqlexpress");
                Port   = MyXml.XmlRead(DbConfigFile, "DbConfig", "Port", (ServerType == DbServerType.MySql) ? "3306" : "");
                WindowsAuthentication = bool.Parse(MyXml.XmlRead(DbConfigFile, "DbConfig", "WindowsAuthentication", false.ToString()));
                UserId         = MyXml.XmlRead(DbConfigFile, "DbConfig", "UserId", "");
                Password       = DES.Decrypt(MyXml.XmlRead(DbConfigFile, "DbConfig", "Password", ""), MES_KEY, MES_IV); //解密
                Database       = MyXml.XmlRead(DbConfigFile, "DbConfig", "Database", "");
                bSuccessLoaded = true;
            }
            catch (Exception ex)
            {
                throw (new Exception(ex.Message));
            }
            return(bSuccessLoaded);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 保存连接参数至配置文件MyDB.xml
        /// </summary>
        /// <returns></returns>
        public bool SaveSettings()
        {
            bool bSuccessSaved = false;

            try
            {
                string DbConfigFile = Directory.GetCurrentDirectory() + "\\" + DB_CONFIG_FILE;

                MyXml.XmlWrite(DbConfigFile, "DbConfig", "ServerType", ServerType.ToString());
                MyXml.XmlWrite(DbConfigFile, "DbConfig", "Server", Server);
                MyXml.XmlWrite(DbConfigFile, "DbConfig", "Port", Port);
                MyXml.XmlWrite(DbConfigFile, "DbConfig", "WindowsAuthentication", WindowsAuthentication.ToString());
                MyXml.XmlWrite(DbConfigFile, "DbConfig", "UserId", UserId);
                MyXml.XmlWrite(DbConfigFile, "DbConfig", "Password", DES.Encrypt(Password, MES_KEY, MES_IV));  //加密
                MyXml.XmlWrite(DbConfigFile, "DbConfig", "Database", Database);
                bSuccessSaved = true;
            }
            catch (Exception ex)
            {
                throw (new Exception(ex.Message));
            }
            return(bSuccessSaved);
        }