protected void Button1_Click(object sender, EventArgs e) { if (this.FileUpload1.HasFile) //判断上传数据是否为空 { //bool FileIsValid=false; String fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();//获取文件类型 String[] restrictExtension = { ".gif", ".jpg", ".bmp", ".png" }; int length = txtName.Text.Trim().Length; int length1 = jf.Text.Trim().Length; if (length != 0) { if (length1 != 0) { if (restrictExtension.Contains(fileExtension) == true) { string filePath = "/JFimages/"; string fileName = filePath + CreatePasswordHash(FileUpload1.FileName, 4) + fileExtension; string mappath = Server.MapPath(fileName); FileUpload1.PostedFile.SaveAs(mappath); //转换存储到服务器上的物理路径 SOSOshop.Model.JF.Jf_Model mdoel = new SOSOshop.Model.JF.Jf_Model(); mdoel.name = this.txtName.Text.Trim(); mdoel.jf = Convert.ToInt32(this.jf.Text.Trim()); mdoel.pt = this.pt.Text.Trim(); mdoel.img = fileName; SOSOshop.BLL.JF.Jf bll = new SOSOshop.BLL.JF.Jf(); bll.Add(mdoel); Response.Write("<script>alert('文件上传成功');window.location.href='Add.aspx';</script>"); //Response.Redirect("js_list.aspx"); } else { Response.Write("<script>alert('请选择正确的图片格式');window.location.href='Add.aspx';</script>"); } } else { Response.Write("<script>alert('请输入商品积分');window.location.href='Add.aspx';</script>"); } } else { Response.Write("<script>alert('请输入商品名称');window.location.href='Add.aspx';</script>"); } } else { Response.Write("<script>alert('请选择图片')</script>"); } }
public void Add(SOSOshop.Model.JF.Jf_Model model) { Database db = DatabaseFactory.CreateDatabase(); StringBuilder sb = new StringBuilder(); sb.Append("insert into Jf values(@Name,@Jf,@Pt,@Img);SELECT @@IDENTITY;"); DbCommand cmd = db.GetSqlStringCommand(sb.ToString()); db.AddInParameter(cmd, "@Name", DbType.String, model.name); db.AddInParameter(cmd, "@Jf", DbType.Int32, model.jf); db.AddInParameter(cmd, "@Pt", DbType.String, model.pt); db.AddInParameter(cmd, "@Img", DbType.String, model.img); try { db.ExecuteScalar(cmd); } catch (Exception e) { throw e; } }