1. 程式人生 > >數據訪問類代碼模塊收集

數據訪問類代碼模塊收集

代碼 () sql lis sel int 分類 body pan

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        }

數據訪問類代碼模塊收集