Exemplo n.º 1
0
        /// <summary>
        /// SQLite数据库配置信息列表
        /// </summary>
        /// <returns></returns>
        private List <DBConfig> GetSQliteDbConfigs()
        {
            List <DBConfig> _configs = null;


            #region 获得数据库配置存储地址
            string DbSettingPath = GetDbSettingPath();
            #endregion

            #region 读取数据库配置
            if (File.Exists(DbSettingPath))
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(DbSettingPath);
                    XmlNode root = doc.DocumentElement;
                    if (root.ChildNodes != null && root.ChildNodes.Count == 1)
                    {
                        XmlNode _node = root.ChildNodes[0];
                        if (_node.ChildNodes != null && _node.ChildNodes.Count > 0)
                        {
                            _configs = new List <DBConfig>();
                            SQLiteDBConfig _config = null;
                            foreach (XmlNode _dbnode in _node.ChildNodes)
                            {
                                if (_dbnode.Name == "DbInfo" && _dbnode.ChildNodes != null && _dbnode.ChildNodes.Count > 0)
                                {
                                    _config = new SQLiteDBConfig();

                                    if (_dbnode.Attributes["IsUs"].InnerText == "1")
                                    {
                                        _config.IsUse = true;
                                    }
                                    foreach (XmlNode _dbdetail in _dbnode.ChildNodes)
                                    {
                                        if (_dbdetail.Name == "File")
                                        {
                                            _config.ServerAddress = _dbdetail.InnerText;
                                        }
                                        if (_dbdetail.Name == "ReMarke")
                                        {
                                            _config.DBReamrke = _dbdetail.InnerText;
                                        }
                                    }

                                    _configs.Add(_config);
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
            #endregion

            return(_configs);
        }
        /// <summary>
        /// 更改数据库存储配置
        /// </summary>
        /// <param name="_DbFilePath"></param>
        /// <returns></returns>
        public bool ChangeDbConfig(string _DbFilePath)
        {
            bool bolSucced = false;

            try
            {
                SQLiteDBConfig _LtDbConfig = new SQLiteDBConfig();
                _LtDbConfig.ServerAddress = _DbFilePath;
                _LtDbConfig.DBReamrke     = "个人数据库";
                _LtDbConfig.DbName        = "IPSdb";
                _LtDbConfig.IsUse         = true;

                List <DBConfig> _configs = DBConfigFactory.Instance.LoadAllDbConfig();
                if (_configs == null)
                {
                    _configs = new List <DBConfig>();
                }
                else
                {
                    foreach (DBConfig _config in _configs)
                    {
                        _config.IsUse = false;
                    }
                }
                _configs.Add(_LtDbConfig);

                DBConfigFactory.Instance.SaveDbConfigs(_configs);
            }
            catch (Exception ex)
            {
            }
            return(bolSucced);
        }
        /// <summary>
        /// 1.用户选择数据存储目录,为用户创建相应的数据库文件和说明书存储目录(内部会判断之前是否已经进行过配置,即同名数据库文件判断)
        /// </summary>
        /// <param name="_DBSaveDirectory">保存数据的目录</param>
        /// <returns>0:保存失败;1:保存成功;2:之前配置过,存在同名的数据库文件</returns>
        public int SaveDbconfig(string _DBSaveDirectory)
        {
            int _state = 0;

            string DbSavePath     = Path.Combine(_DBSaveDirectory, "IPSdb.db3");
            string ImgSaveDirpath = Path.Combine(_DBSaveDirectory, "ImagePath");

            if (!Directory.Exists(ImgSaveDirpath))
            {
                Directory.CreateDirectory(ImgSaveDirpath);
            }
            FileInfo file = new FileInfo(DbSavePath);

            if (file.Exists)
            {
                _state = 2;
            }
            else
            {
                try
                {
                    bool _dbcreateSucced = CreateDataBase(DbSavePath);//创建状态
                    if (_dbcreateSucced)
                    {
                        if (!Directory.Exists(ImgSaveDirpath))
                        {
                            Directory.CreateDirectory(ImgSaveDirpath);
                        }

                        SQLiteDBConfig _LtDbConfig = new SQLiteDBConfig();
                        _LtDbConfig.ServerAddress = DbSavePath;
                        _LtDbConfig.DBReamrke     = "相册信息1";
                        _LtDbConfig.DbName        = "Ablum01";
                        _LtDbConfig.IsUse         = true;

                        List <DBConfig> _configs = new List <DBConfig>();
                        _configs.Add(_LtDbConfig);

                        DBConfigFactory.Instance.SaveDbConfigs(_configs);

                        _state = 1;
                    }
                    else
                    {
                        _state = 0;
                    }
                }
                catch
                {
                    _state = 0;
                }
            }
            return(_state);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 保存数据库配置 (暂时只实现了SQlite数据库配置的保存)
        /// </summary>
        /// <param name="_configs">配置列表</param>
        /// <returns>true:保存成功;false:保存失败</returns>
        public bool SaveDbConfigs(List <DBConfig> _configs)
        {
            bool bolSucced = false;

            if (_configs != null && _configs.Count > 0)
            {
                try
                {
                    string DbSettingPath = GetDbSettingPath();//数据库配置文件存储地址

                    XmlDocument doc = new XmlDocument();

                    doc.LoadXml("<DataBaseConfig></DataBaseConfig>");

                    XmlNode root = doc.DocumentElement;

                    XmlNode MainNode = doc.CreateElement("DataBases");


                    foreach (DBConfig _config in _configs)
                    {
                        #region 添加数据库

                        XmlNode DbNode = doc.CreateElement("DbInfo");

                        if (_config is SQLiteDBConfig)
                        {
                            SQLiteDBConfig _sqliteconfig = (SQLiteDBConfig)_config;
                            XmlAttribute   attri         = doc.CreateAttribute("IsUs");
                            if (_sqliteconfig.IsUse)
                            {
                                attri.Value = "1";
                            }
                            else
                            {
                                attri.Value = "0";
                            }
                            DbNode.Attributes.Append(attri);

                            XmlNode DbFileNode = doc.CreateElement("File");
                            DbFileNode.InnerText = _sqliteconfig.ServerAddress;
                            DbNode.AppendChild(DbFileNode);

                            XmlNode DbRemarkeNode = doc.CreateElement("ReMarke");
                            DbRemarkeNode.InnerText = _sqliteconfig.DBReamrke;
                            DbNode.AppendChild(DbRemarkeNode);
                        }

                        MainNode.AppendChild(DbNode);
                        #endregion
                    }
                    root.AppendChild(MainNode);

                    doc.Save(DbSettingPath);

                    bolSucced = true;
                }
                catch
                {
                }
            }
            return(bolSucced);
        }
        /// <summary>
        /// 2.用户选择数据存储目录,为用户创建相应的数据库文件和说明书存储目录
        /// </summary>
        /// <param name="_DBSaveDirectory">数据文件存储目录</param>
        /// <param name="Isoverride">是否覆盖(如果存在同名的数据库文件)</param>
        /// <returns></returns>
        public bool SaveDbconfig(string _DBSaveDirectory, bool Isoverride)
        {
            bool _Succed = false;

            string        DbFileSaveDirectory = Path.Combine(_DBSaveDirectory, "Database");
            DirectoryInfo dir = new DirectoryInfo(DbFileSaveDirectory);

            if (!dir.Exists)
            {
                dir.Create();
            }

            string   DbSavePath     = Path.Combine(dir.FullName, "IPSdb.db3");
            string   ImgSaveDirpath = Path.Combine(_DBSaveDirectory, "ImagePath");
            FileInfo file           = new FileInfo(DbSavePath);

            if (file.Exists)
            {
                if (Isoverride)
                {
                    file.Delete();                        //删除原文件
                    _Succed = CreateDataBase(DbSavePath); //重新创建数据库文件
                }
            }
            else
            {
                _Succed = CreateDataBase(DbSavePath);//重新创建数据库文件
            }


            if (_Succed)
            {
                try
                {
                    if (!Directory.Exists(ImgSaveDirpath))
                    {
                        Directory.CreateDirectory(ImgSaveDirpath);
                    }

                    SQLiteDBConfig _LtDbConfig = new SQLiteDBConfig();
                    _LtDbConfig.ServerAddress = DbSavePath;
                    _LtDbConfig.DBReamrke     = "个人数据库";
                    _LtDbConfig.DbName        = "IPSdb";
                    _LtDbConfig.IsUse         = true;

                    List <DBConfig> _configs = DBConfigFactory.Instance.LoadAllDbConfig();
                    if (_configs == null)
                    {
                        _configs = new List <DBConfig>();
                    }
                    else
                    {
                        foreach (DBConfig _config in _configs)
                        {
                            _config.IsUse = false;
                        }
                    }
                    _configs.Add(_LtDbConfig);

                    DBConfigFactory.Instance.SaveDbConfigs(_configs);
                }
                catch
                {
                    _Succed = false;
                }
            }

            return(_Succed);
        }
        /// <summary>
        /// SQLite数据库配置信息列表
        /// </summary>
        /// <returns></returns>
        private List<DBConfig> GetSQliteDbConfigs()
        {
            List<DBConfig> _configs = null;

            #region 获得数据库配置存储地址
            string DbSettingPath = GetDbSettingPath();
            #endregion

            #region 读取数据库配置
            if (File.Exists(DbSettingPath))
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(DbSettingPath);
                    XmlNode root = doc.DocumentElement;
                    if (root.ChildNodes != null && root.ChildNodes.Count == 1)
                    {
                        XmlNode _node = root.ChildNodes[0];
                        if (_node.ChildNodes != null && _node.ChildNodes.Count > 0)
                        {
                            _configs = new List<DBConfig>();
                            SQLiteDBConfig _config = null;
                            foreach (XmlNode _dbnode in _node.ChildNodes)
                            {
                                if (_dbnode.Name == "DbInfo" && _dbnode.ChildNodes != null && _dbnode.ChildNodes.Count > 0)
                                {
                                    _config = new SQLiteDBConfig();

                                    if (_dbnode.Attributes["IsUs"].InnerText == "1")
                                    {
                                        _config.IsUse = true;
                                    }
                                    foreach (XmlNode _dbdetail in _dbnode.ChildNodes)
                                    {
                                        if (_dbdetail.Name == "File")
                                        {
                                            _config.ServerAddress = _dbdetail.InnerText;
                                        }
                                        if (_dbdetail.Name == "ReMarke")
                                        {
                                            _config.DBReamrke = _dbdetail.InnerText;
                                        }
                                    }

                                    _configs.Add(_config);
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
            #endregion

            return _configs;
        }