/// <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); }
/// <summary> /// 更新数据库 /// </summary> /// <param name="connection">数据库连接</param> /// <param name="assembly_name">资源库路径, 例如 IMC.Test.DB</param> /// <param name="msg">错误信息</param> /// <returns>当前更新后的数据库版本号</returns> public DBVersion updateDB(DBConnection connection, string assembly_name, ref string msg) { DBVersion current_db_version = getCurrentDBVersion(connection); if (db_version.compare(current_db_version) != 1) {//当前最新版本号小于等于数据库版本号, 无需更新 msg = "当前数据库无需更新!"; return(db_version); } //获取所有数据库升级描述文件 Assembly assembly = null; try { assembly = Assembly.Load(assembly_name); } catch { msg = "无法获取更新描述文件!"; return(db_version); } Type[] types = assembly.GetTypes(); List <DBUpdateProfile> update_profiles = new List <DBUpdateProfile>(); List <DBDataProcess> data_processes = new List <DBDataProcess>(); //获取描述文件后按照版本号先后顺序排列 foreach (Type type in types) { try { if (type.BaseType == typeof(DBUpdateProfile)) {//数据库更新 DBUpdateProfile profile = Activator.CreateInstance(type) as DBUpdateProfile; //过滤掉之前更新过的记录 if (!profile.shouldUpdate(current_db_version)) { continue; } int index = -1; for (int i = 0; i < update_profiles.Count; i++) { if (update_profiles[i].start_db_version.compare(profile.start_db_version) == 1) { index = i; break; } } if (index >= 0) { update_profiles.Insert(index, profile); } else { update_profiles.Add(profile); } } else if (type.BaseType == typeof(DBDataProcess)) {//数据处理操作 DBDataProcess process = Activator.CreateInstance(type) as DBDataProcess; //过滤掉之前更新过的记录 if (!process.shouldPerformProcess(current_db_version)) { continue; } data_processes.Add(process); } } catch { } } //更新数据库 foreach (DBUpdateProfile profile in update_profiles) { List <string> sqls = profile.getUpdateSqlList(connection.db_type); foreach (var query in sqls) { DBQuery.query(query, connection); } //查找是否有相关的数据更新操作, 有则进行更新 foreach (DBDataProcess process in data_processes) { if (process.start_db_version.compare(profile.start_db_version) == 0) { process.processData(connection); } } } //更新版本号到数据库相关表(需继承后实现) msg = updateNewVersionInfoToDB(connection); return(db_version); }