/// <summary> /// 对同一个target循环执行数据记录关联的指令,执行成功后执行sql更新 /// </summary> /// <param name="drs">指令数据数组</param> /// <param name="colmap">字段与指令的映射</param> /// <param name="tpl">指令协议类别:停车场/消费/门禁/考勤/卡务中心</param> /// <param name="cmd">指令命令名称</param> /// <param name="target">通讯目标位置</param> /// <param name="deviceID">设备下位机地址</param> /// <param name="query">数据更新执行实例</param> /// <param name="dbItem">执行更新的数据项</param> /// <returns>成功执行返回空,失败返回错误原因</returns> public static string ExecuteDataRows(DataRow[] drs, string[,] colmap, string tpl, string cmd, CommiTarget target, string deviceID, QueryDataRes query, string dbItem) { if (null == drs || drs.Length < 1 || null == colmap || colmap.GetLength(1) < 2 || string.IsNullOrEmpty(tpl) || string.IsNullOrEmpty(cmd)) return ""; if (null == target || string.IsNullOrEmpty(deviceID)) return ""; //声明一个集合来储存数据 List<DataRow> drList = new List<DataRow>(); string msg = ""; //执行指令 CmdProtocol cmdP = new CmdProtocol(deviceID, false); //循环得到传过来的数据 foreach (DataRow dr in drs) { if (null == dr) continue; string tagdata = "@设备地址=" + deviceID; for (int c = 0; c < colmap.GetLength(0); c++) { object val = dr[colmap[c, 1]]; if (true.Equals(val)) val = "1"; if (false.Equals(val)) val = "0"; tagdata = basefun.setvaltag(tagdata, colmap[c, 0], Convert.ToString(val)); } //设置指令 cmdP.setCommand(tpl, cmd, tagdata); cmdP.ResetState(); //发送指令 CommiManager.GlobalManager.SendCommand(target, cmdP); if (!cmdP.EventWh.WaitOne(2000, false)) { msg = basefun.setvaltag(tagdata, "{状态}", "通讯超时失败!"); break; } if (string.IsNullOrEmpty(cmdP.ResponseFormat)) { msg = basefun.setvaltag(tagdata, "{状态}", tagdata + "指令错误!"); break; } drList.Add(dr); } //更新数据库 //判断数据源和存储过程是否为空 if (null == query || string.IsNullOrEmpty(dbItem)) return msg; //得到数据的数量 Estar.Common.Tools.NameObjectList[] ps = new Estar.Common.Tools.NameObjectList[drList.Count]; for (int i = 0; i < drList.Count; i++) ps[i] = ParamManager.createParam(drList[i]); //得到数据与配置的数据已经保持一致,但发现没有执行存储过程。。。 //执行数据库操作(方法封装在query内) bool success = query.ExecuteNonQuery(dbItem, ps, ps, ps); if (!success) msg = basefun.setvaltag(msg, "{状态}", basefun.valtag(msg, "{状态}") + "更新数据库失败!"); return msg; }
/// <summary> /// 对同一个target循环执行数据记录关联的指令,执行成功后执行sql更新 /// </summary> /// <param name="drs">指令数据数组</param> /// <param name="colmap">字段与指令的映射</param> /// <param name="tpl">指令协议类别:停车场/消费/门禁/考勤/卡务中心</param> /// <param name="cmd">指令命令名称</param> /// <param name="target">通讯目标位置</param> /// <param name="deviceID">设备下位机地址</param> /// <param name="query">数据更新执行实例</param> /// <param name="dbItem">执行更新的数据项</param> /// <returns>成功执行返回空,失败返回错误原因</returns> public static string ExecuteDataRows(DataRow[] drs, string[,] colmap, string tpl, string cmd, CommiTarget target, string deviceID, QueryDataRes query, string dbItem) { if (null == drs || drs.Length < 1 || null == colmap || colmap.GetLength(1) < 2 || string.IsNullOrEmpty(tpl) || string.IsNullOrEmpty(cmd)) { return(""); } if (null == target || string.IsNullOrEmpty(deviceID)) { return(""); } //声明一个集合来储存数据 List <DataRow> drList = new List <DataRow>(); string msg = ""; //执行指令 CmdProtocol cmdP = new CmdProtocol(deviceID, false); //循环得到传过来的数据 foreach (DataRow dr in drs) { if (null == dr) { continue; } string tagdata = "@设备地址=" + deviceID; for (int c = 0; c < colmap.GetLength(0); c++) { object val = dr[colmap[c, 1]]; if (true.Equals(val)) { val = "1"; } if (false.Equals(val)) { val = "0"; } tagdata = basefun.setvaltag(tagdata, colmap[c, 0], Convert.ToString(val)); } //设置指令 cmdP.setCommand(tpl, cmd, tagdata); cmdP.ResetState(); //发送指令 CommiManager.GlobalManager.SendCommand(target, cmdP); if (!cmdP.EventWh.WaitOne(2000, false)) { msg = basefun.setvaltag(tagdata, "{状态}", "通讯超时失败!"); break; } if (string.IsNullOrEmpty(cmdP.ResponseFormat)) { msg = basefun.setvaltag(tagdata, "{状态}", tagdata + "指令错误!"); break; } drList.Add(dr); } //更新数据库 //判断数据源和存储过程是否为空 if (null == query || string.IsNullOrEmpty(dbItem)) { return(msg); } //得到数据的数量 Estar.Common.Tools.NameObjectList[] ps = new Estar.Common.Tools.NameObjectList[drList.Count]; for (int i = 0; i < drList.Count; i++) { ps[i] = ParamManager.createParam(drList[i]); } //得到数据与配置的数据已经保持一致,但发现没有执行存储过程。。。 //执行数据库操作(方法封装在query内) bool success = query.ExecuteNonQuery(dbItem, ps, ps, ps); if (!success) { msg = basefun.setvaltag(msg, "{状态}", basefun.valtag(msg, "{状态}") + "更新数据库失败!"); } return(msg); }