public static List <YangZhuChangMeter> GetYangZhuChangMeters(string xmlSource) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlSource); string buildId = doc.SelectSingleNode("/root/common/building_id").InnerText; string gatewayId = doc.SelectSingleNode("/root/common/gateway_id").InnerText; string type = doc.SelectSingleNode("/root/common/type").InnerText; DateTime collectTime = ToolUtil.GetDateTimeFromString(doc.SelectSingleNode("/root/data/time").InnerText); List <YangZhuChangMeter> meterList = new List <YangZhuChangMeter>(); XmlNodeList nodeList = doc.SelectNodes("/root/data/meters/meter"); foreach (XmlNode item in nodeList) { YangZhuChangMeter tempMeter = new YangZhuChangMeter(); string meterid = item.Attributes["id"].Value; XmlNodeList nodeParams = item.SelectNodes("function"); int meterStatus = 1; foreach (XmlNode paramNode in nodeParams) { string paramName = paramNode.Attributes["id"].Value; string paramError = paramNode.Attributes["error"].Value.Trim(); if (paramName == "EPI" || paramName == "LJLL") { meterStatus = string.IsNullOrEmpty(paramError) ? 1 : 0; decimal paramValue; paramValue = Convert.ToDecimal(paramNode.InnerText); tempMeter.BuildId = buildId; tempMeter.GatewayName = gatewayId; tempMeter.MeterCode = meterid; tempMeter.Time = collectTime; tempMeter.Status = meterStatus; tempMeter.Value = paramValue; } } meterList.Add(tempMeter); } return(meterList); }
public static int InsertRealTimeValues(string connectString, List <YangZhuChangMeter> list) { if (list.Count == 0) { return(0); } StringBuilder builder = new StringBuilder(); StringBuilder builder1 = new StringBuilder(); builder.Append("replace into t_data_realtimevalue(BuildId,GatewayName,MeterCode,Time,Value,Status)"); builder1.Append("replace into t_data_historyvalue(BuildId,GatewayName,MeterCode,Time,Value,Status)"); for (int i = 0; i < list.Count; i++) { YangZhuChangMeter temp = list[i]; if (i == 0) { builder.Append(string.Format(" values('{0}','{1}','{2}','{3}',{4},'{5}')", temp.BuildId, temp.GatewayName, temp.MeterCode, temp.Time.ToString("yyyy-MM-dd HH:mm:00"), temp.Value, temp.Status)); builder1.Append(string.Format(" values('{0}','{1}','{2}','{3}',{4},'{5}')", temp.BuildId, temp.GatewayName, temp.MeterCode, temp.Time.ToString("yyyy-MM-dd HH:mm:00"), temp.Value, temp.Status)); } else { builder.Append(string.Format(" ,('{0}','{1}','{2}','{3}',{4},'{5}')", temp.BuildId, temp.GatewayName, temp.MeterCode, temp.Time.ToString("yyyy-MM-dd HH:mm:00"), temp.Value, temp.Status)); builder1.Append(string.Format(" ,('{0}','{1}','{2}','{3}',{4},'{5}')", temp.BuildId, temp.GatewayName, temp.MeterCode, temp.Time.ToString("yyyy-MM-dd HH:mm:00"), temp.Value, temp.Status)); } } int cnt = 0; using (MySqlConnection connection = new MySqlConnection(connectString)) { connection.Open(); try { MySqlCommand command = new MySqlCommand(builder.ToString(), connection); command.CommandText = builder.ToString(); command.Connection = connection; command.CommandType = System.Data.CommandType.Text; cnt += command.ExecuteNonQuery(); } catch (Exception e) { throw e; } } using (MySqlConnection connection = new MySqlConnection(connectString)) { connection.Open(); try { MySqlCommand command = new MySqlCommand(builder1.ToString(), connection); command.CommandText = builder1.ToString(); command.Connection = connection; command.CommandType = System.Data.CommandType.Text; cnt += command.ExecuteNonQuery(); } catch (Exception e) { throw e; } } return(cnt); }