using SqlSugar; using System; using System.Collections.Generic; using System.Text; namespace ReZero.SuperAPI { public partial class MethodApi { private long _targetDb; public bool SynchronousData(long originalDb, long targetDb, bool? isBak) { _targetDb = targetDb; var odb = App.Db; var tdb = App.GetDbById(targetDb); if (!tdb!.Ado.IsValidConnection()) { new Exception(TextHandler.GetCommonText("目标数据库连接失败", "The target database connection failed")); } tdb!.CurrentConnectionConfig.MoreSettings=odb.CurrentConnectionConfig.MoreSettings; tdb!.CurrentConnectionConfig.ConfigureExternalServices = odb.CurrentConnectionConfig.ConfigureExternalServices; try { tdb!.CodeFirst.InitTables(typeof(ZeroEntityInfo), typeof(ZeroEntityColumnInfo), typeof(ZeroInterfaceCategory), typeof(ZeroInterfaceList), typeof(ZeroDatabaseInfo), typeof(ZeroUserInfo)); tdb!.BeginTran(); var randomNum =Convert.ToInt32( DateTime.Now.ToString("HHmmss")); SynchronousTable(odb, tdb, isBak, randomNum); SynchronousTable(odb, tdb, isBak, randomNum); SynchronousTable(odb, tdb, isBak, randomNum); SynchronousTable(odb, tdb, isBak, randomNum); SynchronousTable(odb, tdb, isBak, randomNum); SynchronousTable(odb, tdb, isBak, randomNum); var tIno = odb.Queryable().First(it => it.Id == targetDb); tdb.Updateable() .SetColumns(it => new ZeroDatabaseInfo { DbType = tIno.DbType, Connection = tIno.Connection, EasyDescription = tIno.EasyDescription, }) .Where(it => it.Id == 1).ExecuteCommand(); tdb.Deleteable().Where(it => it.Id == targetDb).ExecuteCommand(); tdb.CommitTran(); } catch (Exception) { tdb!.CommitTran(); throw; } return true; } private void SynchronousTable(ISqlSugarClient? odb, SqlSugar.SqlSugarClient? tdb, bool? isBak, int randomNum) where T : class, new() { var tTableName = tdb!.EntityMaintenance.GetTableName(); var newtTableName = tTableName.ToLower().Replace("zero_","_bak_") + randomNum; var oldList = odb!.Queryable().ToList(); if (isBak == true) { tdb!.CodeFirst.As(newtTableName).InitTables(); tdb.DbMaintenance.TruncateTable(newtTableName); tdb.Insertable(oldList).AS(newtTableName).ExecuteCommand(); } tdb.DbMaintenance.TruncateTable(); tdb.Insertable(oldList).ExecuteCommand(); } } }