没有数据库,仅供参考逻辑
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using System.Timers;namespace ETL_Task{ public partial class Service1 : ServiceBase { private System.Timers.Timer timer_ETL=new System.Timers.Timer(); private database dbremote = new database(); private string strConnTask = "server=(local);database=DGQX;uid=admin;pwd=1qaz2wsx#edc"; public Service1() { InitializeComponent(); timer_ETL.Elapsed = new System.Timers.ElapsedEventHandler(timer_ETL_Elapsed); timer_ETL.Interval = 5*60*1000;} protected void timer_ETL_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { string strSqlsource = ""; string strSqlcommand = ""; foreach (DataRow dr in dbremote.ReDataTable("select top(1) * from T_ETL_Task where Task_flag= 'false' and Task_Date<'" DateTime.Now.ToString("yyyy-MM-dd HH:mm") "'", "tb_Task", strConnTask).Rows) { try { if (dr["Task_Flag"].ToString().Trim()=="False") { strSqlcommand = " update T_ETL_Task set Task_flag = 'true' where Task_name = '" dr["Task_name"].ToString().Trim() "'"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask);//琐定数据 switch (dr["Time_Field"].ToString().Trim()) { case "@all": strSqlcommand = " delete from " dr["Dest_Table"].ToString().Trim(); strSqlsource = " select " dr["Source_Fields"].ToString().Trim() " from " dr["Source_Table"].ToString().Trim(); break; case "@join": strSqlcommand = " delete from " dr["Dest_Table"].ToString().Trim() " where dCreateDate>='" Convert.ToDateTime(dr["Task_Date"].ToString().Trim()).AddDays(-1).ToString("yyyy-MM-dd") "'"; strSqlsource = " select a.*,b.dCreateDate from " dr["Source_Table"].ToString().Trim(); strSqlsource = strSqlsource.Replace("@date", Convert.ToDateTime(dr["Task_Date"].ToString().Trim()).AddDays(-1).ToString("yyyy-MM-dd")); break; default: strSqlcommand = " delete from " dr["Dest_Table"].ToString().Trim() " where " dr["Time_Field"].ToString().Trim() ">='" Convert.ToDateTime(dr["Task_Date"].ToString().Trim()).AddDays(-1).ToString("yyyy-MM-dd") "'"; strSqlsource = " select " dr["Source_Fields"].ToString().Trim() " from " dr["Source_Table"].ToString().Trim() " where " dr["Time_Field"].ToString().Trim() ">='" Convert.ToDateTime(dr["Task_Date"].ToString().Trim()).AddDays(-1).ToString("yyyy-MM-dd") "'"; break; } dbremote.ExecSqlCommand(strSqlcommand, dr["Dest_Constr"].ToString().Trim());//删除昨天数据 using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") dr["Source_Table"].ToString().Trim() "到" dr["Dest_Table"].ToString().Trim() "开始传输!"); try { strSqlcommand = " insert INTO Task_log(Date_Time,Log_Message) Values('" DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") "','" dr["Dest_Table"].ToString().Trim() "到" dr["Dest_Table"].ToString().Trim() "开始传输!" "')"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask);//写入数据库日志 } catch (Exception ee) { strSqlcommand = " insert INTO Task_log(Date_Time,Log_Message) Values('" DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") "','" ee.Message.ToString() "')"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask); } } //传输昨天数据 try { dbremote.InsertDa(dbremote.ReDataTable(strSqlsource, "tb_source", dr["Source_Constr"].ToString().Trim()), dr["Dest_Constr"].ToString().Trim(), dr["Dest_Table"].ToString().Trim(), dr["Source_Fields"].ToString().Trim()); } catch (Exception ee) { strSqlcommand = " insert INTO Task_log(Date_Time,Log_Message) Values('" DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") "','" ee.Message.ToString() "')"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask); } using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") dr["Source_Table"].ToString().Trim() "到" dr["Dest_Table"].ToString().Trim() "传输完毕!"); } strSqlcommand = " update T_ETL_Task set Task_flag = 'false',Task_Date='" DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") " " dr["Begin_Time"].ToString().Trim() "'" " where Task_name = '" dr["Task_name"].ToString().Trim() "'"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask);//更新任务,下一天执行 try { strSqlcommand = " insert INTO Task_log(Date_Time,Log_Message) Values('" DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") "','" dr["Dest_Table"].ToString().Trim() "到" dr["Dest_Table"].ToString().Trim() "传输完毕!" "')"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask); } catch (Exception ee) { strSqlcommand = " insert INTO Task_log(Date_Time,Log_Message) Values('" DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") "','" ee.Message.ToString() "')"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask); } } } catch (Exception ee) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") ee.Message.ToString().Trim()); } } } } protected override void OnStart(string[] args) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") "Start."); } try { using (System.IO.StreamReader sr = new System.IO.StreamReader(System.Environment.CurrentDirectory "\\ini.txt", true)) { this.strConnTask = Encrypt.Encrypt.DecryptString(sr.ReadLine().ToString().Trim()); } } catch (Exception ee) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") ee.Message); } } string strSqlcommand = ""; try { strSqlcommand = " insert INTO Task_log(Date_Time,Log_Message) Values('" DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") "','working')"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask); } catch (Exception ee) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") ee.Message); } strSqlcommand = " insert INTO Task_log(Date_Time,Log_Message) Values('" DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") "','" ee.Message.ToString() "')"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask); } timer_ETL.Enabled = true; } protected override void OnStop() { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") "Stop."); } string strSqlcommand = ""; try { strSqlcommand = " insert INTO Task_log(Date_Time,Log_Message) Values('" DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") "','stop')"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask); } catch (Exception ee) { strSqlcommand = " insert INTO Task_log(Date_Time,Log_Message) Values('" DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") "','" ee.Message.ToString() "')"; dbremote.ExecSqlCommand(strSqlcommand, strConnTask); } timer_ETL.Enabled = false; dbremote = null; } }}
评论