因开发、测试场景经常需要模拟机生产环境的数据,原同事开发了一个ado.net图形化同步工具,对非技术人员操作友好,但对技术员使用并不方便,每次同步需源库数据与目标的数据源字段进行配置,且同步大数据时慢,因此抽空自己写了简单的同步工具,极大提高了效率。直接贴代码如下:
using System;using System.Collections.Generic;using System.Data;using System.Text;using Oracle.ManagedDataAccess.Client;namespace SyncData{class Program{static void Main(string[] args){//原库连接string strConnSource = System.Configuration.ConfigurationManager.ConnectionStrings["SourceConnectionString"].ConnectionString;//目标连接string strConnTarget = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];//同步方案string[] SyschSchemes = System.Configuration.ConfigurationManager.AppSettings["SyschScheme"].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);OracleConnection conn = null;List list = new List();Console.WriteLine("------准备导入----------------");try{using (conn = new OracleConnection(strConnSource)){for (int i = 0; i < SyschSchemes.Length; i++){string strSql = SyschSchemes[i].ToLower();string strSqlO = SyschSchemes[i];if (strSql.Trim().Length == 0){continue;}string tb = strSql.Substring(strSql.IndexOf("from") + 4, strSql.IndexOf("where") - strSql.IndexOf("from") - 4);using (OracleCommand cmd = conn.CreateCommand()){cmd.CommandText = strSqlO;cmd.CommandType = CommandType.Text;OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(cmd);DataTable dt = new DataTable(tb);oracleDataAdapter.Fill(dt);Console.WriteLine(tb + "共:" + dt.Rows.Count + "条");if (dt.Rows.Count == 0)continue;//暂停该表所有触发器string trigerN =string.Format("alter table {0}disable all triggers",tb);string trigerY = string.Format("alter table {0}enable all triggers", tb);string deleteSql = string.Format("delete from{0}", strSqlO.Substring(strSql.IndexOf("from") + 4));//暂停目标表触发器executeSQL(strConnTarget, trigerN);//删除目标表条件数据executeSQL(strConnTarget, deleteSql);//同步至目标表InsertData(strConnTarget,dt);//启用触发器executeSQL(strConnTarget, trigerY);}}}}catch (Exception ex){Console.WriteLine(ex.Message);}finally{if (conn != null && conn.State == ConnectionState.Open){conn.Close();}}Console.Read();}private static void InsertData(string strConnTarget, DataTable dt){OracleConnection conn = null;StringBuilder strInse = new StringBuilder();Dictionary<string, List
app.config进行数据源库、同步目标库、同步表 配置
sysdate-10; select a,b,b,c,d,f from testb where 1=1; "/>