Exemple #1
0
 /// <summary>
 /// 将数据库连接创建器加入映射表
 /// </summary>
 /// <param name="creator">数据库连接创建器</param>
 public void AddDatabaseCreatory(IFDatabaseCreator creator)
 {
     //检查映射表中是否已经存在该命名的连接
     if (!m_Databases.ContainsKey(creator.Name))
     {
         m_Databases.Add(creator.Name, creator);
     }
 }
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="creator">数据库连接创建器</param>
 public TPCDatabase(IFDatabaseCreator creator)
 {
     m_Creator    = creator;
     m_Connection = m_Creator.CreateConnection();
 }
Exemple #3
0
        /// <summary>
        /// 通过数据库配置文件初始化数据库工厂
        /// </summary>
        /// <param name="config">配置文件</param>
        /// <returns>是否成功初始化</returns>
        public bool LoadConfig(string config)
        {
            //检查数据库配置文件是否存在
            if (!File.Exists(config))
            {
                TPCLogger.Instance.Write(LOGGER_TYPE.LOGGER_ERROR, "Not found database config file.");
                return(false);
            }

            try
            {
                //读取XML配置文件
                XmlDocument doc = new XmlDocument();
                doc.Load(config);
                //读取系统默认数据库连接配置
                XmlNode nodeSystem = doc.SelectSingleNode("databases/system_database");
                if (nodeSystem == null || nodeSystem.Attributes["name"] == null)
                {
                    TPCLogger.Instance.Write(LOGGER_TYPE.LOGGER_ERROR, "Not set system database.");
                    return(false);
                }
                m_SystemDatabaseName = nodeSystem.Attributes["name"].Value;

                //遍历全部数据库连接配置
                XmlNodeList nodes = doc.SelectNodes("databases/database");
                m_Instance.m_Databases.Clear();
                foreach (XmlNode node in nodes)
                {
                    if (node.Attributes["name"] == null || node.Attributes["connection"] == null || node.Attributes["category"] == null)
                    {
                        TPCLogger.Instance.Write(LOGGER_TYPE.LOGGER_ERROR, "Database config file is not match format.");
                        return(false);
                    }
                    string name       = node.Attributes["name"].Value;
                    string connection = node.Attributes["connection"].Value;
                    string category   = node.Attributes["category"].Value;

                    IFDatabaseCreator creator = null;
                    //按照不同的数据库类型生成不同的数据库连接创建器
                    if (category.ToLower().Equals("sqlserver"))
                    {
                    }
                    else if (category.ToLower().Equals("postgresql"))
                    {
                        creator          = new PostgresSQLCreator();
                        creator.Category = DatabaseCategory.PostgresSQL;
                    }
                    else if (category.ToLower().Equals("mysql"))
                    {
                    }
                    else if (category.ToLower().Equals("oracle"))
                    {
                    }
                    else if (category.ToLower().Equals("oledb"))
                    {
                    }
                    else
                    {
                        TPCLogger.Instance.Write(LOGGER_TYPE.LOGGER_WARNNING, "Unknow category database.");
                        continue;
                    }

                    if (creator != null)
                    {
                        creator.Name             = name;
                        creator.ConnectionString = connection;
                        AddDatabaseCreatory(creator);
                        TPCLogger.Instance.Write(LOGGER_TYPE.LOGGER_INFORMATION, string.Format("Create database {0} category:{1}", name, category));
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                TPCLogger.Instance.Write(LOGGER_TYPE.LOGGER_ERROR, e.Message);
                return(false);
            }
        }