private void Save(SWSDataContext db, realrec _realrec) { try { db.realrec.InsertOnSubmit(_realrec); db.SubmitChanges(); } catch (Exception ex) { LogMg.AddError(ex); } }
/// <summary> /// 保存数据到数据库 /// </summary> /// <param name="testid"></param> /// <param name="value"></param> private static void SaveToDatabase(SWSDataContext db, int testid, double value) { LogMg.AddDebug(string.Format("testid={0} value={1}", testid, value)); // SWSDataContext db = new SWSDataContext(Util.ServerSocketHelper.GetConnection(dbname)); test test = db.test.SingleOrDefault(c => c.testid == testid); //查询出检测点 if (test != null) { if (test.means.Trim() == "屏幕取词" || test.means.Trim() == "自动获取") { realrec realrec = new realrec(); realrec.testid = testid; realrec.value = (decimal)value; realrec.testtime = DateTime.Now; realrec.remark = "from 201 Server AutoCollectionThread"; db.realrec.InsertOnSubmit(realrec); db.SubmitChanges(); //提交 } } }
/// <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 void handlerV88Msg(string data) { LogMg.AddDebug("udp协议接收的数据============= " + data); List <String> datas = CaiBao(data); DateTime date = DateTime.Now; List <Dictionary <string, string> > listDir = parseDataV88(datas, ref date); foreach (Dictionary <string, string> item in listDir) { try { //找出设备编号 String stationNo = getStationNo(item); if (String.IsNullOrEmpty(stationNo)) { continue; } //找出编号对应的站点 SWS_DB.guangdai_station_link link = V88StationLink.SingleOrDefault(c => c.wsid == stationNo); if (link == null) { continue; } SWSDataContext sws = new SWSDataContext(ConnectStringHelper.GetConnection(SysConfig.userProfile.DbAddress, link.db_name, SysConfig.userProfile.DbUserName, SysConfig.userProfile.DbPassword)); updateStationLine(sws, (int)link.station_id); foreach (string key in item.Keys) { //数据 KeyValuePair <string, string> data1 = item.Single(c => c.Key == key); try { KeyValuePair <string, int>?keyAndValue = getRegisterByKey(key); if (keyAndValue == null) { continue; } List <gong_kuang_config> gks = sws.gong_kuang_config.Where(c => c.station_id == link.station_id && c.read_register == ((KeyValuePair <string, int>)keyAndValue).Value.ToString()).ToList(); foreach (gong_kuang_config gk in gks) { if (gk == null || gk.testid == null || gk.testid == 0) { continue; } AutoCollectionThread.updateStationOnlineInfo(sws, (int)link.station_id); //保存数据 double multiple = gk.Multiple == null ? 1 : (double)gk.Multiple; double addNumber = gk.AddNumber == null ? 0 : (double)gk.AddNumber; String strValue = data1.Value; if (key.StartsWith("k") || key.StartsWith("K")) { if (strValue == "1") { strValue = "0"; } else { strValue = "1"; } } double value = Double.Parse(strValue) * multiple + addNumber; gk.read_value = value.ToString(); realrec rec = new realrec(); rec.testid = gk.testid; rec.value = (decimal)value; rec.testtime = date; rec.remark = "V88协议的设备数据"; sws.realrec.InsertOnSubmit(rec); } } catch (Exception ex) { LogMg.AddError(ex); } } sws.SubmitChanges(); } catch (Exception ex) { LogMg.AddError(ex); } } }