【代码备份】服务器A、服务器B两台服务器数据库不通的情况,通过TOPSHEF服务进行数据同步
一、解决问题:
服务器A、服务器B两台服务器数据库不通的情况,通过TOPSHEF服务进行数据同步
二、解决思路:
1、从服务器A中将数据转成JSON对象,
2、通过TOPSHELF服务访问服务器B项目网址并传递参数(JSON对象)
3、服务器B接受传递的参数并保存
4、将TOPSHELF服务安装到服务器B上
三、服务器A操作步骤:
1、服务器A新建控制台应用程序(.NET Framework)
2、服务器A引用TOPSHELE服务
2、服务器A引用NewtonsoftJson
3、服务器A关键代码:每秒中向服务器B传递JSON对象
Program.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Data; 5 using System.Net; 6 using Topshelf; 7 using System.Timers; 8 9 namespace TOPSHELF 10 { 11 12 13 public class Program 14 { 15 public static void Main(string[] args) 16 { 17 var rc = HostFactory.Run(x => //1 18 { 19 x.Service<TownCrier>(s => //2 20 { 21 s.ConstructUsing(name => new TownCrier()); //3 22 s.WhenStarted(tc => tc.Start()); //4 23 s.WhenStopped(tc => tc.Stop()); //5 24 }); 25 x.RunAsLocalSystem(); //6 26 27 x.SetDescription("Sample Topshelf Host"); //7 28 x.SetDisplayName("Stuff"); //8 29 x.SetServiceName("Stuff"); //9 30 }); //10 31 32 var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode()); //11 33 Environment.ExitCode = exitCode; 34 } 35 } 36 37 38 public class TownCrier 39 { 40 readonly Timer _timer; 41 public TownCrier() 42 { 43 //_timer = new Timer(1000*60*60*4) { AutoReset = true }; 44 _timer = new Timer(1000) { AutoReset = true }; 45 _timer.Elapsed += _timer_Elapsed; 46 } 47 48 private void _timer_Elapsed(object sender, ElapsedEventArgs e) 49 { 50 string paramStar = GetReport1(); 51 Console.WriteLine(paramStar); 52 byte[] postData = Encoding.UTF8.GetBytes(paramStar); 53 WebClient client = new WebClient(); 54 client.Headers.Add("Content-Type", "application/json"); //采取POST方式必须加的header 55 client.Headers.Add("ContentLength", postData.Length.ToString()); 56 string url = string.Format(Maticsoft.Common.ConfigHelper.GetConfigString("url"), "1"); 57 byte[] responseData = client.UploadData(url, "POST", postData); //得到返回字符流 58 Encoding.UTF8.GetString(responseData); //解码 59 } 60 61 public void Start() { _timer.Start(); } 62 public void Stop() { _timer.Stop(); } 63 64 65 /// <summary> 66 /// 获取供电报表1数据 67 /// </summary> 68 /// <param name="context"></param> 69 public string GetReport1() 70 { 71 //供电月报1 72 Maticsoft.Model.DD_ESREPORT1 model1 = new Maticsoft.Model.DD_ESREPORT1(); 73 List<Maticsoft.Model.DD_ESREPORT1> jsdata1 = new List<Maticsoft.Model.DD_ESREPORT1>(); 74 string strreport1 = ""; 75 string strsql = @"select x.mc railwayname, 76 o.jgmc jgname, 77 t.bbn || '-' || lpad(t.bby, 2, '0') month, 78 t.*, 79 t.rowid 80 from JDBB_JR1MX t 81 left join sjzd_xb x 82 on t.a1 = x.xbbm 83 left join sjzd_xzjg o 84 on t.a2 = o.jgbm 85 where 1 = 1 86 and t.bj_code = '0101000000' 87 and t.bbn = to_number(to_char(sysdate, 'yyyy')) 88 and t.bby = to_number(to_char(sysdate, 'MM')) 89 order by t.bbn desc, t.bby desc, o.jgmc desc,x.mc desc"; 90 DataSet ds = DBUtility.DbHelperOra.Query(strsql); 91 if (ds.Tables[0].Rows.Count > 0) 92 { 93 foreach (DataRow dr in ds.Tables[0].Rows) 94 { 95 model1 = new Maticsoft.Model.DD_ESREPORT1(); 96 model1.ID = ""; 97 model1.DEPARTMENTID = dr["jgname"].ToString(); 98 model1.RAILWAYID = dr["railwayname"].ToString(); 99 model1.A = dr["A3"].ToString(); 100 model1.B = dr["A4"].ToString(); 101 102 model1.C = dr["A5"].ToString(); 103 model1.D = dr["A7"].ToString(); 104 model1.E = dr["A8"].ToString(); 105 model1.F = dr["A9"].ToString(); 106 model1.G = dr["A10"].ToString(); 107 108 model1.H = dr["A14"].ToString(); 109 model1.I = dr["A16"].ToString(); 110 model1.J = dr["A17"].ToString(); 111 model1.K = dr["A18"].ToString(); 112 model1.L = dr["A19"].ToString(); 113 114 model1.M = dr["A20"].ToString(); 115 model1.N = dr["A21"].ToString(); 116 model1.O = dr["A22"].ToString(); 117 model1.P = dr["A23"].ToString(); 118 model1.Q = dr["A24"].ToString(); 119 120 model1.R = dr["A25"].ToString(); 121 model1.S = dr["A26"].ToString(); 122 model1.T = ""; 123 model1.CREATETIME = dr["month"].ToString(); 124 model1.STATE = "1"; 125 jsdata1.Add(model1); 126 } 127 } 128 strreport1 = Newtonsoft.Json.JsonConvert.SerializeObject(jsdata1); 129 return strreport1; 130 } 131 132 } 133 }
4、服务器A关键代码:服务器A配置文件(数据库配置、服务器B访问地址配置)
App.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <startup> 4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> 5 </startup> 6 <appSettings> 7 <add key="ConStringEncrypt" value="false"/> 8 <!--本地--> 9 <add key="ConnectionString" value="Data Source=xxx;User ID=xxx;Password=xxx;"/> 10 <add key="url" value="http://localhost:93/ZZReport/ashx/list.ashx?state={0}"/> 11 </appSettings> 12 </configuration>
四、服务器B操作步骤:
1、服务器B接收服务器A传递过来的参数并保存到服务器B数据库中
list.ashx
1 <%@ WebHandler Language="C#" Class="list" %> 2 3 using System; 4 using System.Collections.Generic; 5 using System.Collections; 6 using System.Linq; 7 using System.Web; 8 using System.Web.UI; 9 using System.Web.UI.WebControls; 10 using System.Data; 11 12 13 public class list : IHttpHandler 14 { 15 16 17 public void ProcessRequest(HttpContext context) 18 { 19 string state = context.Request.Params["state"]; 20 switch (state) 21 { 22 case "1": 23 SaveReport1(context); 24 break; 25 26 } 27 28 } 29 30 private void SaveReport1(HttpContext context) 31 { 32 33 //供电月报1 34 Maticsoft.Model.DD_ESREPORT11 model1 = new Maticsoft.Model.DD_ESREPORT11(); 35 Maticsoft.BLL.DD_ESREPORT11 bll1 = new Maticsoft.BLL.DD_ESREPORT11(); 36 List<Maticsoft.Model.DD_ESREPORT11> jsdata1 = new List<Maticsoft.Model.DD_ESREPORT11>(); 37 38 39 string jgid = ""; 40 string railwayid = ""; 41 string strmonth = ""; 42 PageBase p = new PageBase(); 43 int rvalue = 0; 44 45 using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream)) 46 { 47 string s = sr.ReadToEnd(); 48 try 49 { 50 List<Maticsoft.Model.DD_ESREPORT11> report1list = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<Maticsoft.Model.DD_ESREPORT11>>(s); 51 ArrayList ids = new ArrayList(); 52 53 foreach (var item in report1list) 54 { 55 model1 = new Maticsoft.Model.DD_ESREPORT11(); 56 model1.ID = p.NewGUID; 57 string strsqljgid = "select o.id from JG_ORGANIZATIONG o where o.ORGANIZATIONGNAME=decode('" + item.DEPARTMENTID + "','xxx','" + item.DEPARTMENTID + "', substr('" + item.DEPARTMENTID + "',0,2))"; 58 DataSet dsjgid = Maticsoft.DBUtility.DbHelperOra.Query(strsqljgid); 59 if (dsjgid.Tables[0].Rows.Count > 0) 60 { 61 jgid = dsjgid.Tables[0].Rows[0][0].ToString(); 62 } 63 model1.DEPARTMENTID = jgid; 64 string strrailwayid = "select s.id from sb_railway s where s.railwayname='" + item.RAILWAYID + "'"; 65 DataSet dsrailwayid = Maticsoft.DBUtility.DbHelperOra.Query(strrailwayid); 66 if (dsrailwayid.Tables[0].Rows.Count > 0) 67 { 68 railwayid = dsrailwayid.Tables[0].Rows[0][0].ToString(); 69 } 70 model1.RAILWAYID = railwayid; 71 model1.A = item.A; 72 model1.B = item.B; 73 74 model1.C = item.C; 75 model1.D = item.D; 76 model1.E = item.E; 77 model1.F = item.F; 78 model1.G = item.G; 79 80 model1.H = item.H; 81 model1.I = item.I; 82 model1.J = item.J; 83 model1.K = item.K; 84 model1.L = item.L; 85 86 model1.M = item.M; 87 model1.N = item.N; 88 model1.O = item.O; 89 model1.P = item.P; 90 model1.Q = item.Q; 91 92 model1.R = item.R; 93 model1.S = item.S; 94 model1.T = item.T; 95 model1.CREATETIME = item.CREATETIME; 96 strmonth = model1.CREATETIME; 97 model1.STATE = "1"; 98 99 jsdata1.Add(model1); 100 101 } 102 string strsqlexist = "select t.* from DD_ESREPORT1 t where t.CREATETIME='" + (new DateTime().Year + '-' + new DateTime().Month) + "' and t.DEPARTMENTID='" + jgid + "'"; 103 DataSet dsexist = Maticsoft.DBUtility.DbHelperOra.Query(strsqlexist); 104 if (dsexist.Tables[0].Rows.Count == 0) 105 { 106 rvalue = bll1.AddReport1(jsdata1, jgid, strmonth); 107 if (rvalue > 0) 108 { 109 context.Response.Write("{\"flag\":true}"); 110 } 111 else 112 { 113 context.Response.Write("{\"flag\":false,\"msg\":\"操作失败\"}"); 114 } 115 } 116 else 117 { 118 119 } 120 } 121 catch (Exception ex) 122 { 123 context.Response.Write("{\"flag\":false,\"msg\":\"" + Maticsoft.Common.StringUtil.CheckStr(ex.Message) + "\"}"); 124 } 125 } 126 context.Response.ContentType = "text/json"; 127 context.Response.End(); 128 129 } 130 131 132 public bool IsReusable 133 { 134 get 135 { 136 return false; 137 } 138 } 139 140 }
五、安装到服务器:
1、重新生成项目
2、将当前项目下的BIN文件拷贝到服务器上
3、以管理员身份运行CMD
4、找到程序中BIN/DEBUG/TOPSHELF.EXE目录,并执行这个exe文件
5、打开服务找到stuff服务,点击启动这个服务