两个数据库同步表数据,一分钟一次

Posted 萌橙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了两个数据库同步表数据,一分钟一次相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GT.Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            InItPlan();
            InItPlanDetail();
        }

        public DataRow CreateDataRow(DataTable inserttable,DataRow dr) 
        {
            DataRow tempdr = inserttable.NewRow();
            tempdr["I_PLAN_ID"] = Convert.ToInt32(dr["I_PLAN_ID"]);
            tempdr["E_PLAN_TYPE"] = Convert.ToInt32(dr["E_PLAN_TYPE"]);
            tempdr["E_PLAN_STATE"] = Convert.ToInt32(dr["E_PLAN_STATE"]);
            tempdr["I_PLAN_SUBNO"] = Convert.ToInt32(dr["I_PLAN_SUBNO"]);
            tempdr["I_PLAN_NO"] = Convert.ToInt32(dr["I_PLAN_NO"]);
            tempdr["C_TRANUM"] = dr["C_TRANUM"].ToString();
            tempdr["I_CUTNUM"] = Convert.ToInt32(dr["I_CUTNUM"]);
            tempdr["I_QZ_ID"] = Convert.ToInt32(dr["I_QZ_ID"]);
            tempdr["D_CREATE"] = Convert.ToDateTime(dr["D_CREATE"]);
            tempdr["C_TRANUM_SEND"] = dr["C_TRANUM_SEND"].ToString();
            tempdr["I_LOCOM_ID"] = Convert.ToInt32(dr["I_LOCOM_ID"]);
            tempdr["I_DCZ_ID"] = Convert.ToInt32(dr["I_DCZ_ID"]);
            tempdr["C_DCZ_ID"] = dr["C_DCZ_ID"].ToString();
            tempdr["E_TURN_TYPE"] = Convert.ToInt32(dr["E_TURN_TYPE"]);
            tempdr["D_PL_START"] = Convert.ToDateTime(dr["D_PL_START"]);
            tempdr["D_PL_END"] = Convert.ToDateTime(dr["D_PL_END"]);
            tempdr["D_START"] = Convert.ToDateTime(dr["D_START"]);
            tempdr["D_END"] = Convert.ToDateTime(dr["D_END"]);
            tempdr["I_RANGE_ID"] = Convert.ToInt32(dr["I_RANGE_ID"]);
            tempdr["D_SEND"] = Convert.ToDateTime(dr["D_SEND"]);
            tempdr["C_REMARK"] = dr["C_REMARK"].ToString();
            tempdr["B_PUR_PRINT"] = Convert.ToInt32(dr["B_PUR_PRINT"]);
            tempdr["I_GETNUM"] = Convert.ToInt32(dr["I_GETNUM"]);
            tempdr["I_REMAIN"] = Convert.ToInt32(dr["I_REMAIN"]);
            return tempdr;

        }

        public void InItPlan() 
        {
            string strSql = "select * from DM_PL_PLANLIST WITH (NOLOCK)";
            DataTable dt = ADOTools.ExcuteDataTable(ADOTools.OTHERCONNECTION_STR, strSql);
            string mystrSql = "select * from t_dcd_planlist WITH (NOLOCK)";
            DataTable mydt = ADOTools.ExcuteDataTable(ADOTools.LOCALCONNECTION_STR, mystrSql);
            DataTable inserttable = mydt.Clone();//仅复制表结构

            IEnumerable<DataRow> query2 = dt.AsEnumerable().Except(mydt.AsEnumerable(), DataRowComparer.Default);
            DataTable dt3 = query2.CopyToDataTable();
            foreach (DataRow dr in dt.Rows)
            {
                if (mydt.Rows.Count > 0)
                {
                    DataRow[] drlist = mydt.Select("I_PLAN_ID =" + dr["I_PLAN_ID"].ToString() + "");
                    if (drlist.Length == 0)
                    {
                        inserttable.Rows.Add(CreateDataRow(inserttable, dr));
                    }
                }
                else
                {
                    inserttable.Rows.Add(CreateDataRow(inserttable, dr));
                }

            }
            if (inserttable.Rows.Count > 0)
            {
                ADOTools.BulkInsert(ADOTools.LOCALCONNECTION_STR, inserttable, "t_dcd_planlist");
            }
        
        }

        public void InItPlanDetail()
        {
            string strSql = "select * from DM_PL_PLANTEXT WITH (NOLOCK)";
            DataTable dt = ADOTools.ExcuteDataTable(ADOTools.OTHERCONNECTION_STR, strSql);
            string mystrSql = "select * from t_dcd_plantext WITH (NOLOCK)";
            DataTable mydt = ADOTools.ExcuteDataTable(ADOTools.LOCALCONNECTION_STR, mystrSql);
            DataTable inserttable = mydt.Clone();//仅复制表结构

            IEnumerable<DataRow> query2 = dt.AsEnumerable().Except(mydt.AsEnumerable(), DataRowComparer.Default);
            DataTable dt3 = query2.CopyToDataTable();
            foreach (DataRow dr in dt.Rows)
            {
                if (mydt.Rows.Count > 0)
                {
                    DataRow[] drlist = mydt.Select("I_TEXT_ID =" + dr["I_TEXT_ID"].ToString() + "");
                    if (drlist.Length == 0)
                    {
                        inserttable.Rows.Add(CreateDataRowDetail(inserttable, dr));
                    }
                }
                else
                {
                    inserttable.Rows.Add(CreateDataRowDetail(inserttable, dr));
                }

            }
            if (inserttable.Rows.Count > 0)
            {
                ADOTools.BulkInsert(ADOTools.LOCALCONNECTION_STR, inserttable, "t_dcd_plantext");
            }

        }


        public DataRow CreateDataRowDetail(DataTable inserttable, DataRow dr)
        {
            DataRow tempdr = inserttable.NewRow();
            tempdr["I_PLAN_ID"] = Convert.ToInt32(dr["I_PLAN_ID"]);
            tempdr["I_TEXT_ID"] = Convert.ToInt32(dr["I_TEXT_ID"]);
            tempdr["I_CUT_NO"] = Convert.ToInt32(dr["I_CUT_NO"]);
            tempdr["I_YARD_ID"] = Convert.ToInt32(dr["I_YARD_ID"]);
            tempdr["I_LINE_ID"] = Convert.ToInt32(dr["I_LINE_ID"]);
            tempdr["E_PORT"] = Convert.ToInt32(dr["E_PORT"]);
            tempdr["I_MODE"] = Convert.ToInt32(dr["I_MODE"]);
            tempdr["I_CARNUM"] = Convert.ToInt32(dr["I_CARNUM"]);
            tempdr["E_CUT_STATUS"] = Convert.ToInt32(dr["E_CUT_STATUS"]);
            tempdr["I_REMAIN"] = Convert.ToInt32(dr["I_REMAIN"]);
            tempdr["C_REMARK"] =dr["C_REMARK"].ToString();
            return tempdr;

        }
    }
}

 

以上是关于两个数据库同步表数据,一分钟一次的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server - 同步两个数据库

如何一次性同步内存中的集合与其后备数据库表

mysql数据同步的两个表要完全相同么

怎样用php实现两个mysql数据库的同步

Java代码实现两个数据库之间的数据同步

kettle利用excel文件增量同步一个库的数据(多表一次增量同步)