using System; using System.Data; using System.Data.SqlClient; using System.Text; namespace AdapterTaskService { public class AdapterService { public static string connSPS = string.Empty; public AdapterService() { connSPS = System.Configuration.ConfigurationManager.ConnectionStrings["connSPS"].ToString(); } public void Start() { int ThreadSleep = 60000;//每分钟同步一次 try { ThreadSleep = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ThreadSleep"].ToString()); } catch { } Guid OrgID = Guid.Empty; try { OrgID = new Guid(System.Configuration.ConfigurationManager.AppSettings["OrgID"].ToString()); } catch { } string connTPM = System.Configuration.ConfigurationManager.ConnectionStrings["connTPM"].ToString(); DateTime dtNearlest = GetDtNearlest(); Task parent = new Task(() => { new Task(() => { while (true) { dtNearlest = SyncDataMatch(connTPM, connSPS, OrgID, dtNearlest);//轮循同步 Thread.Sleep(ThreadSleep);//1秒 } }, TaskCreationOptions.AttachedToParent).Start(); }, TaskCreationOptions.LongRunning); parent.Start(); parent.Wait(); } /// /// 数据同步 /// private DateTime SyncDataMatch(string connTPM, string connSPS, Guid OrgID, DateTime dtNearlest) { DataTable Table = new DataTable(); using (SqlConnection connection = new SqlConnection(connTPM)) { try { connection.Open(); string sql = "select * from vw_bi_DJExceptionXLK where crdate is not null "; if (dtNearlest != DateTime.MinValue)//dtNearlest != DateTime.MinValue { sql += " and crdate>='" + dtNearlest + "'"; } sql += " order by crdate desc"; using (SqlCommand Com = new SqlCommand(sql, connection)) { SqlDataAdapter Data = new SqlDataAdapter(Com); Data.Fill(Table); } connection.Close(); } catch { } } if (Table != null && Table.Rows.Count > 0) { try { using (SqlConnection connection = new SqlConnection(connSPS)) { connection.Open(); StringBuilder sb = new StringBuilder();//数据组装 foreach (DataRow item in Table.Rows) { sb.Append(@" INSERT INTO[dbo].[T_BS_RISK_TPM]([ID],[machinecode],[machinename],[usedepartmentname],[processworkerid],[processworkername],[description],[remark],[processresult],[crdate],[USER_ID],[DEPARTMENT_ID],[IS_REPORT],[IS_DELETED],[ORG_ID],[IS_MATCH],ENTITY_ORG_TPYE,FLOW_STATUS,FLOW_SEND_STATUS)VALUES("); //,[MATCH_EXCEPTION],[ENTITY_ORG_TPYE],[FORM_ID],[FLOW_STATUS],[FLOW_SEND_STATUS],[FLOW_ID] ,[CREATE_TIME],[MODIFY_TIME],[CREATER_ID],[MODIFIER_ID] sb.Append("'" + Guid.NewGuid() + "'"); sb.Append("," + (item["machinecode"] != null ? ("'" + item["machinecode"].ToString() + "'") : null)); sb.Append("," + (item["machinename"] != null ? ("'" + item["machinename"].ToString() + "'") : null)); sb.Append("," + (item["usedepartmentname"] != null ? ("'" + item["usedepartmentname"].ToString() + "'") : null)); sb.Append("," + (item["processworkerid"] != null ? ("'" + item["processworkerid"].ToString() + "'") : null)); sb.Append("," + (item["processworkername"] != null ? ("'" + item["processworkername"].ToString() + "'") : null)); sb.Append("," + (item["description"] != null ? "'" + item["description"].ToString() + "'" : null)); sb.Append("," + (item["remark"] != null ? ("'" + item["remark"].ToString() + "'") : null)); sb.Append("," + (item["processresult"] != null ? ("'" + item["processresult"].ToString() + "'") : null)); sb.Append("," + (item["crdate"] != null ? ("'" + Convert.ToDateTime(item["crdate"].ToString()) + "'") : ("'" + DateTime.Now + "'"))); sb.Append(",null,null,0,0,'" + OrgID + "',0,0,0,0"); sb.AppendLine(")"); } using (SqlCommand Com = new SqlCommand(sb.ToString(), connection)) { int result = Com.ExecuteNonQuery(); Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]同步数据条数[" + result.ToString() + "]"); } connection.Close(); } dtNearlest = Convert.ToDateTime(Table.Rows[0]["crdate"]).AddSeconds(1);//更新最新时间 } catch (Exception ex) { Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]流程报错[" + ex.Message + "]"); } } else { Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]无同步数据"); } return dtNearlest; } /// /// 获取最近的时间 /// /// /// private DateTime GetDtNearlest() { DateTime dtNearlest = DateTime.MinValue; DataTable Table = new DataTable(); using (SqlConnection connection = new SqlConnection(connSPS)) { try { string sql = "select top 1 crdate from T_BS_RISK_TPM where crdate is not null order by T_BS_RISK_TPM.crdate desc"; connection.Open(); using (SqlCommand Com = new SqlCommand(sql, connection)) { SqlDataAdapter Data = new SqlDataAdapter(Com); Data.Fill(Table); } connection.Close(); } catch { } } if (Table != null && Table.Rows.Count > 0) { dtNearlest = Convert.ToDateTime(Table.Rows[0]["crdate"]).AddSeconds(1);//时间多一点 } return dtNearlest; } } }