首页 > 数据库 >不同服务器数据库,数据表数据同步方案

不同服务器数据库,数据表数据同步方案

时间:2024-01-30 17:22:06浏览次数:33  
标签:string tempTable 数据库 tableName targetDb 数据表 var 服务器 sourceDataTable

代码片段:

  1 using DataSync.Core;
  2 using Furion.Logging.Extensions;
  3 using Microsoft.Data.SqlClient;
  4 using Microsoft.Extensions.Logging;
  5 using System.Data;
  6 namespace DataSync.Application.DataSync.Services
  7 {
  8     public class DataSyncServices : IDataSyncData, ITransient
  9     {
 10         private readonly object lockObj = new object();
 11         /// <summary>
 12         /// 客户端向服务端同步
 13         /// </summary>
 14         /// <param name="clientConn"></param>
 15         /// <param name="serviceConn">目标数据库</param>
 16         /// <returns></returns>
 17         public string SyncDataForClient(string clientConn, string serviceConn)
 18         {
 19             return SyncData(clientConn, serviceConn);
 20         }
 21         /// <summary>
 22         /// 服务端向客户端同步
 23         /// </summary>
 24         /// <param name="serviceConn"></param>
 25         /// <param name="clientConn"></param>
 26         /// <returns></returns>
 27         public string SyncDataForServer(string serviceConn, string clientConn)
 28         {
 29             return SyncData(serviceConn, clientConn);
 30         }
 31         /// <summary>
 32         /// 数据同步
 33         /// </summary>
 34         private string SyncData(string sourceConn, string targetConn)
 35         {
 36             try
 37             {
 38 
 39                 //源数据库 数据源链接
 40                 SqlSugarScope sourceDb = new SqlSugarScope(new ConnectionConfig()
 41                 {
 42                     DbType = SqlSugar.DbType.SqlServer,
 43                     ConnectionString = sourceConn,
 44                     IsAutoCloseConnection = true,
 45                     AopEvents = new AopEvents
 46                     {
 47                         OnLogExecuting = (sql, ps) =>
 48                         {
 49 #if DEBUG
 50                             Log.Information($"语句:{sql},参数:{(ps.Any() ? "[" : string.Empty) + string.Join("|", ps.Select(m => $"{m.ParameterName}={m.Value}")) + (ps.Any() ? "]" : string.Empty)}");
 51 #endif
 52                         }
 53                     }
 54                 });
 55                 //目标数据库 数据源链接
 56                 SqlSugarScope targetDb = new SqlSugarScope(new ConnectionConfig()
 57                 {
 58                     DbType = SqlSugar.DbType.SqlServer,
 59                     ConnectionString = targetConn,
 60                     IsAutoCloseConnection = true,
 61                     AopEvents = new AopEvents
 62                     {
 63                         OnLogExecuting = (sql, ps) =>
 64                         {
 65 #if DEBUG
 66                             Log.Information($"语句:{sql},参数:{(ps.Any() ? "[" : string.Empty) + string.Join("|", ps.Select(m => $"{m.ParameterName}={m.Value}")) + (ps.Any() ? "]" : string.Empty)}");
 67 #endif
 68                         }
 69                     }
 70                 });
 71                 //使用sqlsugar 初始化目标数据库
 72                 targetDb.DbMaintenance.CreateDatabase();
 73                 var tableNames = sourceDb.DbMaintenance.GetTableInfoList(false).Select(t => t.Name).ToList(); // 调用函数获取所有表名
 74                 var syncBlackTable = App.GetConfig<List<string>>("SyncBlackTable");
 75                 tableNames = tableNames.Except(syncBlackTable).ToList();
 76                 //多线程
 77                 Parallel.ForEach(tableNames, tableName =>
 78                 {
 79                     lock (lockObj)
 80                     {
 81                         // 根据表名从源数据库中获取数据并存入 DataTable
 82                         var targetdataTable = DataTableHelper.FetchDataFromTable(tableName, sourceDb);
 83                         //判断数据表在目标库是否存在
 84                         var flagTargetTab = targetDb.DbMaintenance.IsAnyTable(tableName);
 85                         if (!flagTargetTab)
 86                         {
 87                             // 创建表的SQL语句
 88                             var createTableSql = $"CREATE TABLE {tableName} (";
 89                             if (targetdataTable != null && targetdataTable.Rows.Count > 0)
 90                             {
 91                                 //目标数据库写入-先移除数据同步标识
 92                                 DataBaseInfoService.DatatableRemoveCloumns(targetdataTable);
 93                                 // 遍历DataTable的列
 94                                 foreach (DataColumn column in targetdataTable.Columns)
 95                                 {
 96                                     string columnName = column.ColumnName;
 97                                     string dataType = DataBaseInfoService.GetSqlDataType(column.DataType);
 98 
 99                                     createTableSql += $"{columnName} {dataType}, ";
100                                 }
101                                 createTableSql = createTableSql.TrimEnd(',', ' ') + ")";
102                                 // 创建表
103                                 targetDb.Ado.ExecuteCommand(createTableSql);
104 
105                                 ("TargetTable : " + tableName + ",创建成功").LogInformation();
106                                 //    }
107                                 //}
108                             }
109                         }
110                         //AppSys
111                         if (tableName.ToUpper().Equals("APPSYS"))
112                         {
113                             AppSysDataSync.SyncData(tableName, sourceDb, targetDb);
114                         }
115                         var selectCountSql = $"SELECT COUNT(*) FROM {tableName} ";
116 
117                         var sourceCount = sourceDb.Ado.GetInt(selectCountSql);
118 
119                         var middleCount = targetDb.Ado.GetInt(selectCountSql);
120                         //增量
121                         if (sourceCount > middleCount)
122                         {
123                             //  commandTarget.Connection = connTarget;
124                             // commandTarget.CommandType = CommandType.Text;
125                             //查询数据
126                             var selectTableSql = $"SELECT * FROM {tableName}";
127                             //创建datatable(源数据)
128                             var sourceDataTable = sourceDb.Ado.GetDataTable(selectTableSql);
129 
130                             if (sourceDataTable != null && sourceDataTable.Rows.Count > 0)
131                             {
132                                 //新增列 MD5
133                                 DataBaseInfoService.DataTableAddColumsMd5(sourceDataTable);
134                             }
135                             //创建datatable(目标表数据)
136                             var targetDataTable = targetDb.Ado.GetDataTable(selectTableSql);
137 
138                             if (targetDataTable != null && targetDataTable.Rows.Count > 0)
139                             {
140                                 //新增列 MD5
141                                 DataBaseInfoService.DataTableAddColumsMd5(targetDataTable);
142                             }
143                             // 计算差集
144                             var tempTable = new DataTable();
145                             var tempExceptTable = (from source in sourceDataTable.AsEnumerable()
146                                                    where
147                                                    !(from target in targetDataTable.AsEnumerable() select target.Field<string>("MD5")).Contains(
148                                                    source.Field<string>("MD5"))
149                                                    select source);
150                             if (tempExceptTable != null && tempExceptTable.Count() > 0)
151                             {
152                                 tempTable = tempExceptTable.CopyToDataTable();
153                             }
154                             //批量插入数据
155                             if (tempTable != null && tempTable.Rows.Count > 0)
156                             {
157                                 //目标数据库写入-先移除数据同步标识,MD5标识
158                                 DataBaseInfoService.DatatableRemoveCloumns(tempTable);
159                                 var connTarget = new SqlConnection(targetConn);
160                                 DataBaseInfoService.DataBulkCopy(connTarget, tableName, tempTable);
161 
162                                 //  TargetDataScope.Db.Fastest<DataTable>().AS(tableName).BulkCopy(tempTable);
163                             }
164                         }
165                         //删除
166                         else if (sourceCount < middleCount)
167                         {
168                             //查询数据
169                             var selectTableSql = $"SELECT * FROM {tableName}";
170                             //创建datatable(源数据)
171                             var sourceDataTable = sourceDb.Ado.GetDataTable(selectTableSql);
172 
173                             if (sourceDataTable != null && sourceDataTable.Rows.Count > 0)
174                             {
175                                 //新增列 MD5
176                                 DataBaseInfoService.DataTableAddColumsMd5(sourceDataTable);
177                             }
178                             //创建datatable
179                             var taergetTable = targetDb.Ado.GetDataTable(selectTableSql);
180                             if (taergetTable != null && taergetTable.Rows.Count > 0)
181                             {
182                                 //新增列 MD5
183                                 DataBaseInfoService.DataTableAddColumsMd5(taergetTable);
184                             }
185                             // 计算差集
186                             var tempTable = new DataTable();
187                             var tempExceptTable = (from target in taergetTable.AsEnumerable()
188                                                    where
189                                                    !(from source in sourceDataTable.AsEnumerable() select source.Field<string>("MD5")).Contains(
190                                                    target.Field<string>("MD5"))
191                                                    select target);
192                             if (tempExceptTable != null && tempExceptTable.Count() > 0)
193                             {
194                                 tempTable = tempExceptTable.CopyToDataTable();
195                             }
196                             if (tempTable != null && tempTable.Rows.Count > 0)
197                             {
198                                 //获取主键字段
199                                 var PrimaryKeyName = targetDb.DbMaintenance.GetPrimaries(tableName);
200                                 //DataTableHelper.GetPrimaryKeyFieldName(tableName, connTarget);
201                                 //获取自增列
202                                 var Identities = targetDb.DbMaintenance.GetIsIdentities(tableName);
203                                 if (PrimaryKeyName != null && PrimaryKeyName.Count > 0)
204                                 {
205                                     foreach (DataRow row in tempTable.Rows)
206                                     {
207                                         var deleteDataSql = DataTableHelper.ConstructDeleteSql(tableName, PrimaryKeyName, Identities, row);
208                                         //$"DELETE FROM {tableName} WHERE {PrimaryKeyName} ='{row[PrimaryKeyName[0]]}'";
209                                         //目标数据数据操作对象
210                                         targetDb.Ado.ExecuteCommand(deleteDataSql);
211 
212                                     }
213                                 }
214                             }
215                         }
216                         //更新
217                         else
218                         {
219                             //判断是否存在需要更新的记录
220                             //和目标表比较取差集
221                             //查询数据
222                             var selectTableSql = $"SELECT * FROM {tableName}";
223                             //创建datatable(源数据)
224                             var sourceDataTable = sourceDb.Ado.GetDataTable(selectTableSql);
225                             if (sourceDataTable != null && sourceDataTable.Rows.Count > 0)
226                             {
227                                 //新增列 MD5
228                                 DataBaseInfoService.DataTableAddColumsMd5(sourceDataTable);
229                             }
230                             //创建datatable(目标表数据)
231                             var targetDataTable = targetDb.Ado.GetDataTable(selectTableSql);
232                             if (targetDataTable != null && targetDataTable.Rows.Count > 0)
233                             {
234                                 //新增列 MD5
235                                 DataBaseInfoService.DataTableAddColumsMd5(targetDataTable);
236                             }
237                             // 计算差集
238                             var tempTable = new DataTable();
239                             var tempExceptTable = (from source in sourceDataTable.AsEnumerable()
240                                                    where
241                                                    !(from target in targetDataTable.AsEnumerable() select target.Field<string>("MD5")).Contains(
242                                                    source.Field<string>("MD5"))
243                                                    select source);
244                             if (tempExceptTable != null && tempExceptTable.Count() > 0)
245                             {
246                                 tempTable = tempExceptTable.CopyToDataTable();
247                             }
248                             if (tempTable != null && tempTable.Rows.Count > 0)
249                             {
250                                 //删除标识列和MD5列
251                                 DataBaseInfoService.DatatableRemoveCloumns(tempTable);
252                                 //获取目标表主键字段
253                                 var PrimaryKeyName = targetDb.DbMaintenance.GetPrimaries(tableName);
254                                 //获取自增列
255                                 var Identities = targetDb.DbMaintenance.GetIsIdentities(tableName);
256                                 //DataTableHelper.GetPrimaryKeyFieldName(tableName, connTarget);
257                                 foreach (DataRow dataRow in tempTable.Rows)
258                                 {
259                                     var updateDataSql = DataTableHelper.ConstructUpdateSql(tableName, PrimaryKeyName, Identities, dataRow);
260                                     targetDb.Ado.ExecuteCommand(updateDataSql);
261 
262                                 }
263                             }
264                         }
265                     }
266                 });
267             }
268             catch (Exception ex)
269             {
270                 ("Error occurred while connecting to database or fetching data from tables.\n" + ex.Message).LogError();
271                 return "同步失败。详见错误日志!";
272             }
273             return "同步完成!";
274         }
275 
276     }
277 }

Gitee下载地址:https://gitee.com/ltf_free/sync-data.git

标签:string,tempTable,数据库,tableName,targetDb,数据表,var,服务器,sourceDataTable
From: https://www.cnblogs.com/100234ltf/p/17997538

相关文章

  • ContosoRetailDW数据库恢复问题
    https://www.microsoft.com/en-us/download/details.aspx?id=18279下载ContosoBIdemoBAK.exe和ContosoBIdemoABF.exe解压后,无法恢复数据库的问题sqlserverexpress2008直接gui恢复时,提示标题:MicrosoftSQLServerManagementStudio------------------------------路径......
  • window server 服务器ssh远程相关命令
    一、Windows下Shell命令概述Windows的Shell命令又是Windows的CMD命令。而cmd命令又是原来MS-DOS系统保留下来。Shell来源路径与配置对应的命令资源配置在windows的环境变量中:WindowsShell命令是基于配置好的Path环境变量,对Shell命令在Path路径中依次从前至后搜寻到对应命名的......
  • Zabbix升级MySQL数据库的故障
    收到一个MySQL安全漏洞告警,需要修复OracleMySQLJAN2024CriticalPatchUpdate因此想着升级MySQL,步骤如下:sudoapt-getupdatesudoapt-getupgrademysql-serversudosystemctlrestartmysqlmysql--version完成后Zabbix网页界面能打开,但是提示错误如下:TheZabbixdatabaseve......
  • 服务器登录安全方案
    1.生成复杂root密码#opensslrand-base6412a+uJ29c3ECR0BMAv2.生成sshkey#ssh-keygen-N''-b2048-trsa-fsrv-2-96srv-2-96srv-2-96.pub3.sshkey公匙拷贝到目标服务器#[email protected].目标服务器变更ssh登录配置......
  • 服务器需要使用第三方系统时需要登录验证
    创建第三方登录认证和凭证信息HttpHosttargetHost=newHttpHost(host,newInteger(port).intValue(),"http");CredentialsProvidercredentialsProvider=newBasicCredentialsProvider();credentialsProvider.setCredentials(newAuthScope(targetHost.getHostName(),targ......
  • 【极简】conda同一个服务器上迁移环境 export / create
    导出直接看conda的document:https://docs.conda.io/projects/conda/en/latest/commands/env/export.htmlcondaenvexportcondaenvexport--fileSOME_FILE重建condadocumentation:https://docs.conda.io/projects/conda/en/latest/commands/env/create.htmlcondaenvc......
  • 使用Java编写HTTP客户端和服务器:一场与网络的欢乐共舞
    你是否曾经想过,如果有一天你可以和网络对话,那会是怎样的场景?好消息,Java给了你这个机会!今天,我们要一起探讨如何使用Java编写HTTP客户端和服务器,让你和网络的互动变得更加有趣和欢乐。首先,我们需要了解HTTP是什么。简单来说,HTTP就是“超文本传输协议”,它就像是我们与网络交流的语言。......
  • 从零搭建Go语言Web服务器
    从零搭建Go语言Web服务器原创 Go先锋 Go先锋 2024-01-3011:19 发表于广东 听全文Go先锋读完需要9分钟速读仅需3分钟  一、Go语言的优势1.执行效率高Go语言以其出色的执行效率而闻名,这得益于其静态类型和编译型的特性。通过直接编译成机器码,Go程序......
  • 把git当作一个小型最终一致性的 json 数据库
    这几天写了一些有趣的代码:把git当作json数据库。做法是这样的:创建一个git仓库为每个最小粒度的数据创建一个独立的json文件({table}.json)客户端通过Python写git操作代码,实现几个数据库操作接口。数据库操作接口最小集:初始化:把git仓库拉下来(这个后面可以优化为只拉取指......
  • 这些Shell分析服务器日志命令集锦,优秀!
    自己的小网站跑在阿里云的ECS上面,偶尔也去分析分析自己网站服务器日志,看看网站的访问量。看看有没有黑客搞破坏!于是收集,整理一些服务器日志分析命令,大家可以试试!1、查看有多少个IP访问:awk'{print$1}'log_file|sort|uniq|wc-l2、查看某一个页面被访问的次数:grep"/index.ph......