菜品管理模块01
1、 编写菜品数据访问类
- 获取菜品分类
- 新增菜品(返回新增菜品ID号,为了给图片编号)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data.SqlClient; 6 using System.Data; 7 using Models; 8 9 namespace DAL 10 { 11 /// <summary> 12 /// 菜品数据访问类 13 /// </summary> 14 public class DishService 15 { 16 /// <summary> 17 /// 获取菜品分类 18 /// </summary> 19 /// <returns></returns> 20 public List<DishCategroy> GetAllCategory() 21 { 22 string sql = "select CategoryId,CategoryName from DishCategory"; 23 List<DishCategroy> list = new List<DishCategroy>(); 24 SqlDataReader objReader = SQLHelper.GetReader(sql); 25 while (objReader.Read()) 26 { 27 list.Add(new DishCategroy() 28 { 29 CategoryId = Convert.ToInt32(objReader["CategoryId"]), 30 CategoryName = objReader["CategoryName"].ToString().Trim() 31 }); 32 } 33 objReader.Close(); 34 return list; 35 } 36 37 /// <summary> 38 /// 新增菜品(返回新增菜品ID号) 39 /// </summary> 40 /// <param name="objDish"></param> 41 /// <returns></returns> 42 public int AddDish(Dish objDish) 43 { 44 string sql = "insert into Dishes(DishName,UnitPirce,CategoryId)"; 45 sql += " values(@DishName,@UnitPirce,@CategoryId);select @@identity"; 46 SqlParameter[] param = new SqlParameter[] 47 { 48 new SqlParameter("@DishName",objDish.DishName), 49 new SqlParameter("@UnitPirce",objDish.UnitPrice), 50 new SqlParameter("@CategoryId",objDish.CategoryId) 51 }; 52 return Convert.ToInt32(SQLHelper.GetSingleResult(sql)); //返回自增号 53 } 54 55 } 56 }
2、页面载入后台程序编写
- 编写“添加菜品”的后台下拉框填充程序
- 判断是修改还是新增
3、按钮事件的编写
- 数据验证
- 封装对象
- 提交数据
- 提交菜品信息,并返回菜品编号
- 上传图片(采用函数调用的方法,另编一个私有函数)
- 判断是否有文件
- 获取文件大小,判断文件大小是否符合要求
- 获取图片的名称,并修改成规范的文件名
- 上传图片
4.清空以前输入的内容
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using Models; 8 using DAL; 9 10 namespace HotelWebProject.Adminhyl 11 { 12 public partial class DishesPublish : System.Web.UI.Page 13 { 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 if (!IsPostBack) 17 { 18 //初始化菜品分类下拉框 19 this.ddlCategory.DataValueField = "CategoryId"; 20 this.ddlCategory.DataTextField = "CategoryName"; 21 this.ddlCategory.DataSource = new DAL.DishService().GetAllCategory(); 22 this.ddlCategory.DataBind(); 23 } 24 //判断是修改还是新增 25 if (Request.QueryString["Operation"] == "0") //如果是新增, 来自链接项<li><a href="/Adminhyl/Dishes/DishesPublish.aspx?Operation=0">添加菜品</a></li> 26 { 27 this.btnEdit.Visible = false; //编辑按钮不可见 28 } 29 else 30 { 31 32 } 33 this.ltaMsg.Text = "";//ltaMsg可能会有一些信息,每次都清除一下 34 } 35 36 protected void btnPublish_Click(object sender, EventArgs e) 37 { 38 //数据验证 39 if (this.txtDishName.Text.Trim().Length == 0) 40 { 41 this.ltaMsg.Text = "<script>alert('请输入菜品名称!')</script>"; 42 return; 43 } 44 if (this.ddlCategory.SelectedIndex == -1) 45 { 46 this.ltaMsg.Text = "<script>alert('请选择菜品分类!')</script>"; 47 return; 48 } 49 if (this.txtUnitPrice.Text.Trim().Length==0) 50 { 51 this.ltaMsg.Text = "<script>alert('请输入菜品价格!')</script>"; 52 return; 53 } 54 if (!Common.DataValidate.IsInteger(this.txtUnitPrice.Text.Trim())) //用正则表达式验证是否正整数 55 { 56 this.ltaMsg.Text = "<script>alert('菜品价格必须是整数!')</script>"; 57 return; 58 } 59 //封装对象 60 Models.Dish objDish = new Dish() 61 { 62 DishName=this.txtDishName.Text.Trim(), 63 UnitPrice=Convert.ToInt32(this.txtUnitPrice.Text.Trim()), 64 CategoryId=Convert.ToInt32(this.ddlCategory.SelectedValue),//选中菜品分类所对应的ID 65 }; 66 //提交数据 67 try 68 { 69 if (this.btnPublish.Visible == true) //如果是新增菜品 70 { 71 if (!this.fulImage.HasFile) //必须要选择图片(文件名不能为空) 72 { 73 this.ltaMsg.Text = "<script>alert('请选择菜品图片!')</script>"; 74 return; 75 } 76 //提交菜品信息,并返回菜品编号 77 int dishId = new DishService().AddDish(objDish); 78 //上传图片 79 this.UploadImage(dishId); 80 this.ltaMsg.Text = "<script>alert('添加成功!')</script>"; 81 //清空以前输入的内容 82 this.txtDishName.Text = ""; 83 this.txtUnitPrice.Text = ""; 84 this.ddlCategory.SelectedIndex = -1; 85 86 } 87 } 88 catch (Exception ex) 89 { 90 this.ltaMsg.Text = "<script>alert('添加失败!" + ex.Message + " ')</script>"; 91 } 92 93 } 94 /// <summary> 95 /// 上传图片的函数 96 /// </summary> 97 /// <param name="dishId"></param> 98 private void UploadImage(int dishId) 99 { 100 //[1]判断是否有文件 101 if (!this.fulImage.HasFile) return; 102 //[2] 获取文件大小,判断文件大小是否符合要求 103 double fileLength = this.fulImage.FileContent.Length / (1024.0 * 1024.0); 104 if (fileLength > 2.0) 105 { 106 this.ltaMsg.Text = "<script>alert('图片最大不能超过2M')</script>"; 107 return; 108 } 109 //[3]获取图片的名称,并修改成规范的文件名 110 string fileName = this.fulImage.FileName; 111 if (fileName.Substring(fileName.LastIndexOf(".")).ToLower() != ".png") 112 { 113 this.ltaMsg.Text = "<script>alert('图片必须是png格式!')</script>"; 114 return; 115 } 116 fileName = dishId + ".png"; 117 //[4]上传图片 118 try 119 { 120 string path = Server.MapPath("~/Images/dish"); 121 this.fulImage.SaveAs(path + "/" + fileName); 122 } 123 catch (Exception ex) 124 { 125 this.ltaMsg.Text = "<script>alert('图片上传失败!" + ex.Message + " ')</script>"; 126 } 127 } 128 } 129 }