/// <summary> /// 增加一条数据 /// </summary> public int Insert(Model.Section model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into [Section]("); strSql.Append("IsDelete,LevelNum,Name,ParentId,Num,OrderNum,Sort,LeaderId,Ext1,Ext2"); strSql.Append(") values ("); strSql.Append("@IsDelete,@LevelNum,@Name,@ParentId,@Num,@OrderNum,@Sort,@LeaderId,@Ext1,@Ext2"); strSql.Append(") "); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@IsDelete", SqlDbType.TinyInt, 1), new SqlParameter("@LevelNum", SqlDbType.Int, 4), new SqlParameter("@Name", SqlDbType.NVarChar, 50), new SqlParameter("@ParentId", SqlDbType.Int, 4), new SqlParameter("@Num", SqlDbType.VarChar, 50), new SqlParameter("@OrderNum", SqlDbType.VarChar, 100), new SqlParameter("@Sort", SqlDbType.Int, 4), new SqlParameter("@LeaderId", SqlDbType.Int, 4), new SqlParameter("@Ext1", SqlDbType.VarChar, 50), new SqlParameter("@Ext2", SqlDbType.VarChar, 50) }; parameters[0].Value = model.IsDelete; parameters[1].Value = model.LevelNum; parameters[2].Value = model.Name; parameters[3].Value = model.ParentId; parameters[4].Value = model.Num; parameters[5].Value = model.OrderNum; parameters[6].Value = model.Sort; parameters[7].Value = model.LeaderId; parameters[8].Value = model.Ext1; parameters[9].Value = model.Ext2; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.Section model) { StringBuilder strSql = new StringBuilder(); Collection <SqlParameter> parameters = new Collection <SqlParameter> (); SqlParameter parameter = null; int count = 0; strSql.Append("update [Section] set "); parameter = new SqlParameter("@Id", SqlDbType.Int, 4); parameter.Value = model.Id; parameters.Add(parameter); if (model.IsDelete_IsEdit) { strSql.Append(" IsDelete = @IsDelete,"); parameter = new SqlParameter("@IsDelete", SqlDbType.TinyInt, 1); parameter.Value = model.IsDelete; parameters.Add(parameter); count++; } if (model.LevelNum_IsEdit) { strSql.Append(" LevelNum = @LevelNum,"); parameter = new SqlParameter("@LevelNum", SqlDbType.Int, 4); parameter.Value = model.LevelNum; parameters.Add(parameter); count++; } if (model.Name_IsEdit) { strSql.Append(" Name = @Name,"); parameter = new SqlParameter("@Name", SqlDbType.NVarChar, 50); parameter.Value = model.Name; parameters.Add(parameter); count++; } if (model.ParentId_IsEdit) { strSql.Append(" ParentId = @ParentId,"); parameter = new SqlParameter("@ParentId", SqlDbType.Int, 4); parameter.Value = model.ParentId; parameters.Add(parameter); count++; } if (model.Num_IsEdit) { strSql.Append(" Num = @Num,"); parameter = new SqlParameter("@Num", SqlDbType.VarChar, 50); parameter.Value = model.Num; parameters.Add(parameter); count++; } if (model.OrderNum_IsEdit) { strSql.Append(" OrderNum = @OrderNum,"); parameter = new SqlParameter("@OrderNum", SqlDbType.VarChar, 100); parameter.Value = model.OrderNum; parameters.Add(parameter); count++; } if (model.Sort_IsEdit) { strSql.Append(" Sort = @Sort,"); parameter = new SqlParameter("@Sort", SqlDbType.Int, 4); parameter.Value = model.Sort; parameters.Add(parameter); count++; } if (model.LeaderId_IsEdit) { strSql.Append(" LeaderId = @LeaderId,"); parameter = new SqlParameter("@LeaderId", SqlDbType.Int, 4); parameter.Value = model.LeaderId; parameters.Add(parameter); count++; } if (model.Ext1_IsEdit) { strSql.Append(" Ext1 = @Ext1,"); parameter = new SqlParameter("@Ext1", SqlDbType.VarChar, 50); parameter.Value = model.Ext1; parameters.Add(parameter); count++; } if (model.Ext2_IsEdit) { strSql.Append(" Ext2 = @Ext2,"); parameter = new SqlParameter("@Ext2", SqlDbType.VarChar, 50); parameter.Value = model.Ext2; parameters.Add(parameter); count++; } if (count > 0) { strSql.Remove(strSql.Length - 1, 1); } if (count == 0) { return(false); } strSql.Append(" where Id=@Id "); // 将参数泛形集合转传为数组 SqlParameter[] args = new SqlParameter[parameters.Count]; parameters.CopyTo(args, 0); int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), args); if (rows > 0) { return(true); } else { return(false); } }