public static List <ProtocolAttributeModel> DT2ProtocolAttributeModel(DataTable _dt) { List <ProtocolAttributeModel> list = new List <ProtocolAttributeModel>(); if (_dt != null && _dt.Rows.Count > 0) { for (int i = 0; i < _dt.Rows.Count; i++) { ProtocolAttributeModel model = new ProtocolAttributeModel(); model.PAID = _dt.Rows[i]["PAID"].ToString(); model.AType = _dt.Rows[i]["AType"].ToString(); model.PID = _dt.Rows[i]["PID"].ToString(); model.CName = _dt.Rows[i]["CName"].ToString(); model.EName = _dt.Rows[i]["EName"].ToString(); model.IsLoop = Convert.ToBoolean(_dt.Rows[i]["IsLoop"]); model.A2LoopEName = _dt.Rows[i]["A2LoopEName"].ToString(); model.A2Bool = Convert.ToBoolean(_dt.Rows[i]["A2Bool"]); model.A2BoolEName = _dt.Rows[i]["A2BoolEName"].ToString(); model.A2Str = _dt.Rows[i]["A2Str"].ToString(); model.A2StrEName = _dt.Rows[i]["A2StrEName"].ToString(); model.Index = Convert.ToInt32(_dt.Rows[i]["Index"]); list.Add(model); } } return(list); }
/// <summary> /// 更新属性 /// </summary> /// <param name="_list"></param> /// <returns></returns> public static bool UpdateAttributes(ProtocolAttributeModel _model) { string sql = $@"SELECT [PAID],[PID],[AType],[CName],[EName],[IsLoop],[A2LoopEName],[A2Bool],[A2BoolEName],[A2Str],[A2StrEName],[Index] FROM [ProtocolAttribute] WHERE [PAID]='{_model.PAID}'"; DataTable dt = helper.ExecuteQuery(sql); if (dt != null && dt.Rows.Count == 1) { //编辑 //编辑 sql = $@"UPDATE [ProtocolAttribute] SET [PID] = '{_model.PID}' ,[AType]='{_model.AType}' ,[CName] = '{_model.CName}' ,[EName] = '{_model.EName}' ,[IsLoop] = '{_model.IsLoop}' ,[A2LoopEName] = '{_model.A2LoopEName}' ,[A2Bool] = '{_model.A2Bool}' ,[A2BoolEName] = '{_model.A2BoolEName}' ,[A2Str] = '{_model.A2Str}' ,[A2StrEName] = '{_model.A2StrEName}' ,[Index] = {_model.Index} WHERE [PAID] = '{_model.PAID}'" ; } else { //添加 sql = $@"INSERT INTO [ProtocolAttribute] ([PAID] ,[PID] ,[CName] ,[EName] ,[AType] ,[IsLoop] ,[A2LoopEName] ,[A2Bool] ,[A2BoolEName] ,[A2Str] ,[A2StrEName] ,[Index]) VALUES ('{_model.PAID}' ,'{_model.PID}' ,'{_model.CName}' ,'{_model.EName}' ,'{_model.AType}' ,'{_model.IsLoop}' ,'{_model.A2LoopEName}' ,'{_model.A2Bool}' ,'{_model.A2BoolEName}' ,'{_model.A2Str}' ,'{_model.A2StrEName}' ,{_model.Index})" ; } return(helper.ExecuteNonQuery(sql) > 0); }
/// <summary> /// 保存当前协议 /// </summary> private void SaveProto() { if (selectedNode == null) { return; } if (selectedNode.TagType != NodeSelectTag.TAGTYPE.Protocol) { MessageBox.Show("当前状态下不能进行保存,请正确选择需要保存的协议"); return; } if (txtProtoCode.Text.Trim() == "-1" || string.IsNullOrEmpty(this.txtProtoCode.Text.Trim())) { MessageBox.Show("请输入协议编码 只能是数字"); txtProtoCode.Focus(); return; } int protoCode = 0; if (!int.TryParse(this.txtProtoCode.Text, out protoCode)) { MessageBox.Show("协议编码 只能是数字"); txtProtoCode.Focus(); return; } if (string.IsNullOrEmpty(this.txtProtoEnName.Text.Trim())) { MessageBox.Show("请输入协议英文名称"); txtProtoEnName.Focus(); return; } if (string.IsNullOrEmpty(this.txtProtoCnName.Text.Trim())) { MessageBox.Show("请输入协议中文名称"); txtProtoCnName.Focus(); return; } ProtocolModel currModel = selectedNode.Model as ProtocolModel; currModel.Code = protoCode; currModel.EName = txtProtoEnName.Text.Trim(); currModel.CName = txtProtoCnName.Text.Trim(); currModel.Desc = txtProtoDesc.Text.Trim(); for (int i = 0; i < dvGrid.Rows.Count - 1; i++) { ProtocolAttributeModel attModel = new ProtocolAttributeModel(); attModel.AType = dvGrid.Rows[i].Cells[0].Value == null ? "" : dvGrid.Rows[i].Cells[0].Value.ToString(); attModel.EName = dvGrid.Rows[i].Cells[1].Value == null ? "" : dvGrid.Rows[i].Cells[1].Value.ToString(); attModel.CName = dvGrid.Rows[i].Cells[2].Value == null ? "" : dvGrid.Rows[i].Cells[2].Value.ToString(); attModel.IsLoop = dvGrid.Rows[i].Cells[3].Value == null ? false : Convert.ToBoolean(dvGrid.Rows[i].Cells[3].Value); attModel.A2LoopEName = dvGrid.Rows[i].Cells[4].Value == null ? "" : dvGrid.Rows[i].Cells[4].Value.ToString(); attModel.A2BoolEName = dvGrid.Rows[i].Cells[5].Value == null ? "" : dvGrid.Rows[i].Cells[5].Value.ToString(); attModel.A2Bool = dvGrid.Rows[i].Cells[6].Value == null ? false : Convert.ToBoolean(dvGrid.Rows[i].Cells[6].Value); attModel.A2StrEName = dvGrid.Rows[i].Cells[7].Value == null ? "" : dvGrid.Rows[i].Cells[7].Value.ToString(); attModel.A2Str = dvGrid.Rows[i].Cells[8].Value == null ? "" : dvGrid.Rows[i].Cells[8].Value.ToString(); attModel.PAID = dvGrid.Rows[i].Cells[9].Value == null?Guid.NewGuid().ToString() : dvGrid.Rows[i].Cells[9].Value.ToString(); attModel.PID = dvGrid.Rows[i].Cells[10].Value == null ? currModel.PID : dvGrid.Rows[i].Cells[10].Value.ToString(); if (!ProtocolDB.UpdateAttributes(attModel)) { MessageBox.Show($"{attModel.CName} 更新失败"); } } if (ProtocolDB.UpdateProtocol(currModel)) { ShowInfo(InfoType.Succeed, "保存成功"); } else { ShowInfo(InfoType.Error, "协议更改失败"); } selectedNode.Model = currModel; selectedNode.Target.Text = $"[协议]{ currModel.Code}-{currModel.CName}"; myTree.Refresh(); }