/// <summary> /// 保存数据 /// </summary> /// <param name="model"></param> /// <param name="list"></param> /// <returns></returns> public static bool SaveData(UnitGroupModel model, IList<UnitGroupDetailModel> list) { ArrayList sqlList = new ArrayList(); if (!model.ID.HasValue) {// 新增 sqlList.Add(UnitGroupDBHelper.InsertCommand(model)); } else {// 修改 sqlList.Add(UnitGroupDBHelper.UpdateCommand(model)); sqlList.Add(UnitGroupDetailDBHelper.DeleteCommand(model.CompanyCD, model.GroupUnitNo)); } foreach (UnitGroupDetailModel info in list) { sqlList.Add(UnitGroupDetailDBHelper.InsertCommand(info)); } if (SqlHelper.ExecuteTransWithArrayList(sqlList)) { return true; } else { return false; } }
/// <summary> /// 修改数据记录 /// </summary> /// <param name="model">实体类</param> /// <returns>修改操作的执行命令</returns> public static SqlCommand UpdateCommand(UnitGroupModel model) { // SQL语句 SqlCommand comm = new SqlCommand(); comm.CommandText = C_UPDATE; // 参数设置 SqlParameter[] parameters = SetInsertAndUpdateParameters(); parameters = SetInsertAndUpdateParametersValue(parameters, model); comm.Parameters.AddRange(parameters); return comm; }
/// <summary> /// 设置新增和修改的参数数组的值 /// </summary> /// <param name="parameters">参数数组</param> /// <param name="model">实体类</param> /// <returns>返回参数数组</returns> private static SqlParameter[] SetInsertAndUpdateParametersValue(SqlParameter[] parameters, UnitGroupModel model) { if (!model.ID.HasValue) parameters[m_iDCol].Value = System.DBNull.Value; else parameters[m_iDCol].Value = model.ID; // 主键,自动生成 parameters[m_companyCDCol].Value = model.CompanyCD; // 企业代码 parameters[m_groupUnitNoCol].Value = model.GroupUnitNo; // 计量单位组编号 parameters[m_groupUnitNameCol].Value = model.GroupUnitName; // 计量单位组名 if (!model.BaseUnitID.HasValue) parameters[m_baseUnitIDCol].Value = System.DBNull.Value; else parameters[m_baseUnitIDCol].Value = model.BaseUnitID; // 基本计量单位ID(对应计量单位表) parameters[m_remarkCol].Value = model.Remark; // 备注 return parameters; }
/// <summary> /// 插入操作的执行命令 /// </summary> /// <param name="model">实体类</param> /// <returns>插入操作的执行命令</returns> public static SqlCommand InsertCommand(UnitGroupModel model) { // SQL语句 SqlCommand comm = new SqlCommand(); comm.CommandText = C_INSERT + " SET @IndexID = @@IDENTITY "; // 参数设置 SqlParameter[] parameters = SetInsertAndUpdateParameters(); parameters = SetInsertAndUpdateParametersValue(parameters, model); comm.Parameters.AddRange(parameters); SqlParameter IndexID = new SqlParameter("@IndexID", SqlDbType.Int); IndexID.Direction = ParameterDirection.Output; comm.Parameters.Add(IndexID); return comm; }