数据访问类代码模块收集

1、获取分类

 1        /// <summary>
 2        /// 获取菜品分类
 3        /// </summary>
 4        /// <returns></returns>
 5        public List<DishCategroy> GetAllCategory()
 6        {
 7            string sql = "select CategoryId,CategoryName from DishCategory";
 8            List<DishCategroy> list = new List<DishCategroy>();
 9            SqlDataReader objReader = SQLHelper.GetReader(sql);
10            while (objReader.Read()) 
11            {
12                list.Add(new DishCategroy()
13                {
14                    CategoryId = Convert.ToInt32(objReader["CategoryId"]),
15                    CategoryName = objReader["CategoryName"].ToString().Trim()
16                });
17            }
18            objReader.Close();
19            return list;
20        }

2、新增菜品(返回新增菜品ID号)

 1        /// <summary>
 2        /// 新增菜品(返回新增菜品ID号)
 3        /// </summary>
 4        /// <param name="objDish"></param>
 5        /// <returns></returns>
 6        public int AddDish(Dish objDish)
 7        {
 8            string sql = "insert into Dishes(DishName,UnitPrice,CategoryId)";
 9            sql += " values(@DishName,@UnitPrice,@CategoryId);select @@identity";
10            SqlParameter[] param = new SqlParameter[]
11            {
12                   new SqlParameter("@DishName",objDish.DishName),
13                   new SqlParameter("@UnitPrice",objDish.UnitPrice),
14                   new SqlParameter("@CategoryId",objDish.CategoryId)
15            };
16            return Convert.ToInt32(SQLHelper.GetSingleResult(sql,param));  //返回自增号
17        }

3、根据菜品编号获取菜品对象

 1        /// <summary>
 2        /// 根据菜品编号获取菜品对象
 3        /// </summary>
 4        /// <param name="dishId"></param>
 5        /// <returns></returns>
 6            public Dish GetDishById(string dishId)
 7            {
 8              string sql="select DishName,UnitPrice,CategoryId from  Dish where DishId=@DishId";
 9             SqlParameter[]  param=new SqlParameter[]
10             {
11                  new SqlParameter("@DishId",dishId),
12             };
13                Dish objDish=null;
14                SqlDataReader objReader=SQLHelper.GetReader(sql,param);
15                if(objReader.Read())
16                {
17                  objDish=new Dish()
18                  {
19                    DishId=Convert.ToInt32(objReader["DishId"]),
20                    CategoryName=objReader["CategoryName"].ToString(),
21                    CategoryId=Convert.ToInt32(objReader["CategoryId"]),
22                    DishName=objReader["DishName"].ToString(),
23                    UnitPrice=Convert.ToInt32(objReader["UnitPrice"])                
24                  };
25                }
26                objReader.Close();
27                return objDish;
28            }
29        }

 4、根据菜品编号修改菜品信息

       /// <summary>
       /// 根据菜品编号修改菜品信息
       /// </summary>
       /// <param name="objDish"></param>
       /// <returns></returns>
           public int ModifyDish(Dish objDish)
           {
               string sql = "update Dishes set DishName=@DishName,UnitPrice=@UnitPrice,CategoryId=@CategoryId  where DishId=@DishId";
               SqlParameter[] param = new SqlParameter[]
               {
               new SqlParameter("@DishName",DishName),
               new SqlParameter("@UnitPrice",UnitPrice),
               new SqlParameter("@CategoryId",CategoryId),
               new SqlParameter("@DishId",DishId),
               };
               return SQLHelper.Update(sql, param);
           }

 

posted @ 2017-12-27 22:23  爱踢两键  阅读(195)  评论(0编辑  收藏  举报