Exemplo n.º 1
0
        public static DBInitDataProfile fromXMLFile(string path)
        {
            DBInitDataProfile profile = new DBInitDataProfile();

            try
            {
                TextReader tr = File.OpenText(path);
                profile.XMLContent = tr.ReadToEnd();
                tr.Close();
            }
            catch { }
            return(profile);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 初始化数据库
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="reference_path"></param>
        /// <returns></returns>
        public static string initialize(DBConnection connection, string reference_path)
        {
            string msg = createDatabase(connection);

            if (msg != "")
            {
                return(msg);
            }
            List <DBTable> tables = new List <DBTable>();

            try
            {
                string[] table_paths = Directory.GetFiles(reference_path, "*.table");
                foreach (string table_p in table_paths)
                {
                    DBTable tb = DBTable.fromXmlFile(table_p);
                    if (tb.table_name != "")
                    {
                        tables.Add(tb);
                    }
                }
            }
            catch { }
            if (tables.Count == 0)
            {
                msg = "无法获取可用的表描述文件!";
                return(msg);
            }
            string table_err = "";

            foreach (DBTable table in tables)
            {
                table_err += createTable(table, connection);
            }
            msg += table_err;
            //初始化数据库版本号
            DBInitDataConfig config = new DBInitDataConfig();

            config.loadFrom(reference_path);
            if (config.CurrentDBVersion != "1.0.0")
            {
                var query = "insert into " + config.configuration_table_name + " (" + config.configuration_key_column + "," + config.configuration_value_column + ") values ('" + config.configuration_db_version_key + "','" + config.CurrentDBVersion + "')";
                DBQuery.execute(query, connection);
            }
            //初始化数据
            List <DBInitDataProfile> data = new List <DBInitDataProfile>();

            try
            {
                string[] data_paths = Directory.GetFiles(reference_path, "*.data");
                foreach (string data_p in data_paths)
                {
                    DBInitDataProfile dp = DBInitDataProfile.fromXMLFile(data_p);
                    if (dp.table_name.Length > 0)
                    {
                        data.Add(dp);
                    }
                }
            }
            catch { }
            if (data.Count > 0)
            {
                foreach (DBInitDataProfile profile in data)
                {
                    profile.insertAllData();
                }
            }
            return(msg);
        }