/// <summary> /// 将json字符串转换成对象, 再把对象保存到数据库中 /// </summary> /// <param name="json"></param> /// <returns></returns> private bool Save(string json, Socket socket) { bool isSuccess = true; string stationName = ""; //分厂名称 C_To_S_Data <CSDataStandard.Transfer.RealRec> c_to_s_data = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <CSDataStandard.Transfer.RealRec> >(json); Clazz.Config.XML_Org _org = SysConfig.orgConfig.GetOrgByOrgId(c_to_s_data.OrgId); if (_org == null) { //将信息写入到日志文件中 orgid为***的污水厂不存在 LogMg.AddError(string.Format("OrgId:{0}不存在", c_to_s_data.OrgId)); isSuccess = false; } else { try { SWSDataContext SWS = new SWSDataContext(Util.ServerSocketHelper.GetConnection(_org.DBName)); //建立一个分厂数据源提供程序实例 //查找站点名称 country_station _station = SWS.country_station.SingleOrDefault(c => c.id == c_to_s_data.StationId); //更新站点的IP _station.ip = ((System.Net.IPEndPoint)socket.RemoteEndPoint).Address.ToString(); if (_station != null) { stationName = _station.name; //站点名称 } //遍历数据 并把数据添加到数据库中 List <realrec> listrealrec = new List <realrec>(); List <testrec> listtestrec = new List <testrec>(); foreach (CSDataStandard.Transfer.RealRec item in c_to_s_data.Data) { test test = SWS.test.SingleOrDefault(c => c.testid == item.TestId); if (test == null) { LogMg.AddError(string.Format("testid为 {0} 的检测点不存在", item.TestId)); //记录日志 isSuccess = false; } else { if (test.means.Trim() == "屏幕取词" || test.means.Trim() == "自动获取") { realrec _realrec = new realrec(); _realrec.testid = item.TestId; _realrec.testtime = item.TestTime; _realrec.value = (decimal)item.Value; listrealrec.Add(_realrec); } if (test.means.Trim() == "检测录入") { testrec _testrec = SWS.testrec.SingleOrDefault(c => c.testid == item.TestId && c.testtime == item.TestTime); //判断检测当前数据在数据库中是否已经存在 if (_testrec == null) { _testrec = new testrec(); _testrec.testid = item.TestId; _testrec.testtime = item.TestTime; _testrec.value = (decimal)item.Value; listtestrec.Add(_testrec); } else { _testrec.testid = item.TestId; _testrec.testtime = item.TestTime; _testrec.value = (decimal)item.Value; } } } } SWS.realrec.InsertAllOnSubmit(listrealrec); SWS.testrec.InsertAllOnSubmit(listtestrec); SWS.SubmitChanges(); isSuccess = true; } catch (Exception ex) { isSuccess = false; //把错误信息输出到日志文件中 LogMg.AddError(ex.ToString()); DEBUG.ThrowException(ex); } } //lock (lb_msg) //{ // if (lb_msg.Items.Count > 200) // lb_msg.Items.Clear(); // this.lb_msg.Items.Add(string.Format("时间:{0} 客户端:{1} 数据行数{2} 保存{3}", DateTime.Now.ToString(), stationName, c_to_s_data.Data.Count, isSuccess ? "成功" : "失败")); // } SaveClientIp(socket, c_to_s_data); return(isSuccess); }
private bool Save(string json, Socket RSocket) { bool isSuccess = true; string stationName = ""; //站点名称 C_To_S_Data <CSDataStandard.Transfer.RealRec> c_to_s_data = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <CSDataStandard.Transfer.RealRec> >(json); Clazz.Config.XML_Org _org = SysConfig.orgConfig.GetOrgByOrgId(c_to_s_data.OrgId); if (_org == null) { //将信息写入到日志文件中 orgid为***的污水厂不存在 LogMg.AddError("OrgId:\"{0}\"不存在"); isSuccess = false; } else { try { SWSDataContext SWS = new SWSDataContext(Util.ServerSocketHelper.GetConnection(_org.DBName)); //建立一个分厂数据源提供程序实例 //查找站点名称 country_station _station = SWS.country_station.SingleOrDefault(c => c.id == c_to_s_data.StationId); if (_station != null) { stationName = _station.name; //站点名称 } //遍历数据 并把数据添加到数据库中 List <testrec> list = new List <testrec>(); List <test> listTest = new List <test>(); foreach (CSDataStandard.Transfer.RealRec item in c_to_s_data.Data) { //判断检测当前数据在数据库中是否已经存在 if (SWS.realrec.SingleOrDefault(c => c.testid == item.TestId && c.testtime == item.TestTime) == null) { testrec _testrec = new testrec(); _testrec.testid = item.TestId; _testrec.testtime = item.TestTime; _testrec.value = (decimal)item.Value; list.Add(_testrec); } //修改test表 SWS.ExecuteCommand(string.Format("update test set [value]={0} where testid={1}", item.Value, item.TestId)); } SWS.testrec.InsertAllOnSubmit(list); SWS.SubmitChanges(); isSuccess = true; } catch (Exception ex) { isSuccess = false; //把错误信息输出到日志文件中 LogMg.AddError(ex.ToString()); DEBUG.ThrowException(ex); } } lock (lb_msg) { if (lb_msg.Items.Count > 200) { lb_msg.Items.Clear(); } this.lb_msg.Items.Add(string.Format("时间:{0} 客户端:{1} 数据行数{2} 保存{3}", DateTime.Now.ToString(), stationName, c_to_s_data.Data.Count, isSuccess ? "成功" : "失败")); } return(isSuccess); }