public static bool CheckConnection(BaseConfigInfo bci, out string msg) { msg = ""; try { string connectionString = bci.DBConnectionString; string selectDbType = bci.DBType; connectionString = connectionString.Replace("{$App}", AppDomain.CurrentDomain.BaseDirectory); IDbDriver driver = CreateDbDriver(selectDbType); using (IConnection conn = driver.CreateConnection(connectionString)) { SqlStatement st = new SqlStatement("SELECT 1"); conn.QueryScalar(st); } return(true); } catch (Exception ex) { if (ex.Message.Trim().ToUpper().StartsWith("ORA-00923")) { return(true); } else { msg = ex.Message; return(false); } } }
public void ExcuteSQL(BaseConfigInfo bci, string updateFile) { if (updateFile != "") { string connectionString = bci.DBConnectionString.Replace("{$App}", AppDomain.CurrentDomain.BaseDirectory); XmlDocument doc = new XmlDocument(); doc.Load(updateFile); foreach (XmlNode node in doc.SelectNodes("/Update/Database")) { IDbDriver dbDriver = CreateDbDriver(bci.DBType); if (dbDriver == null) { continue; } //开始处理 int success = 0; int errors = 0; using (IConnection conn = dbDriver.CreateConnection(connectionString)) { foreach (XmlNode sub in node.SelectNodes("Sql")) { if (sub == null || String.IsNullOrEmpty(sub.InnerText) || String.IsNullOrEmpty(sub.InnerText.Trim())) { continue; } //读取SQL语句,逐一执行 SqlStatement sql = new SqlStatement(); sql.CommandType = System.Data.CommandType.Text; sql.SqlClause = sub.InnerText.Trim(); dbDriver.FormatSQL(sql); try { conn.Update(sql); success++; } catch (Exception ex) { //出现了错误,我们继续执行 We7.Framework.LogHelper.WriteFileLog(We7.Framework.LogHelper.sql_plugin_update, "执行SQL:" + sql.SqlClause, ex.Message); errors++; continue; } } } We7.Framework.LogHelper.WriteFileLog(We7.Framework.LogHelper.sql_plugin_update.ToString(), "执行完毕:", string.Format("{3}执行完毕!共执行语句{0}条,成功{1},失败{2} 。", success + errors, success, errors, updateFile)); } } }
/// <summary> /// 执行数据迁移 /// </summary> /// <param name="xmlPath">数据结构映射XML文件存放路径(需要确保两个库的结构相同)</param> /// <param name="oldConfig">源数据库</param> /// <param name="newConfig">目标数据库</param> public static void DoMigrate(string xmlPath, BaseConfigInfo oldConfig, BaseConfigInfo newConfig, List <string> tables) { //导入数据 string newConnString = newConfig.DBConnectionString.Replace("{$App}", AppDomain.CurrentDomain.BaseDirectory); IDbDriver driver = Installer.CreateDbDriver(newConfig.DBType); using (IConnection newConn = driver.CreateConnection(newConnString)) { if (Directory.Exists(xmlPath)) { DirectoryInfo dir = new DirectoryInfo(xmlPath); FileInfo[] files = dir.GetFiles("*.xml"); foreach (FileInfo file in files) { string xmlFile = file.FullName; ObjectAssistant oa = new ObjectAssistant(); if (oldConfig.DBConnectionString != "") { oldConfig.DBConnectionString = oldConfig.DBConnectionString.Replace("{$App}", AppDomain.CurrentDomain.BaseDirectory); oa.LoadDBConnectionString(oldConfig.DBConnectionString, oldConfig.DBDriver); } oa.LoadFromFile(xmlFile); //过滤:找出两个数据库都有的表对象 IDbDriver oldDriver = Installer.CreateDbDriver(oldConfig.DBType); IConnection oldConn = oldDriver.CreateConnection(oldConfig.DBConnectionString.Replace("{$App}", AppDomain.CurrentDomain.BaseDirectory)); List <string> objects = GetUpdateObjects(xmlFile, newConn, oldConn, tables); if (objects.Count > 0) { //进行数据转换 foreach (string tpName in objects) { if (tpName == "") { continue; } Type tp = Type.GetType(tpName); Type mt = typeof(MigrateObject <>); Type gt = mt.MakeGenericType(new Type[] { tp }); IMigrateObject mo = Activator.CreateInstance(gt) as IMigrateObject; mo.Connection = newConn; mo.Assistant = oa; mo.Update(); } } } } } }
protected SessionBase(ConnectionScope connectionScope, IDbDriver dbDriver) { ConnectionScope = connectionScope; DbDriver = dbDriver; Connection = dbDriver.CreateConnection(); if (ConnectionScope == ConnectionScope.PerSession) { if (Log.IsDebug) { Log.Debug(LogMessages.Session_OpeningConnection); } Connection.Open(); } }
protected SessionBase(ConnectionScope connectionScope, IDbDriver dbDriver) { this.ConnectionScope = connectionScope; this.DbDriver = dbDriver; this.Connection = dbDriver.CreateConnection(); if (this.ConnectionScope == ConnectionScope.PerSession) { if (Log.IsDebug) { Log.Debug(LogMessages.Session_OpeningConnection); } this.Connection.Open(); } }
public static void ExecuteSQL(BaseConfigInfo bci, string file) { if (file != "" && File.Exists(file)) { XmlDocument doc = new XmlDocument(); doc.Load(file); foreach (XmlNode node in doc.SelectNodes("/Update/Database")) { IDbDriver driver = CreateDbDriver(bci.DBType); if (driver == null) { continue; } int success = 0; int errors = 0; using (IConnection conn = driver.CreateConnection(bci.DBConnectionString)) { foreach (XmlNode sub in node.SelectNodes("Sql")) { SqlStatement sql = new SqlStatement(); sql.CommandType = CommandType.Text; sql.SqlClause = sub.InnerText; driver.FormatSQL(sql); try { conn.Update(sql); success++; } catch (Exception ex) { We7.Framework.LogHelper.WriteFileLog(We7.Framework.LogHelper.sql_update, "执行SQL:" + sql.SqlClause + "\n\t 出现错误:", ex.Message); errors++; continue; } } } We7.Framework.LogHelper.WriteFileLog(We7.Framework.LogHelper.sql_update, "执行完毕:", string.Format("{3}执行完毕!共执行语句{0}条,成功{1}条,失败{2}条", success + errors, success, errors, file)); } } }