牛腩购物32:完成整个网站的功能制作(后台产品列表,多表查询+分页),产品修改保存,图片的上传保存

查询商品表,和分类别,获取商品的类别名称,需要用到多表查询+分页

/// <summary>多表查询分页获取数据列表

        /// </summary>

        /// <param name="tblName">多个表 例如:news a inner join  category b on a.caId=b.id and a.caId=b.id</param>

        /// <param name="strGetFields">多个字段 例如:a.id,a.title,a.caid,a.createtime,a.content,b.name</param>

        /// <param name="strOrder">排序字段</param>

        /// <param name="strOrderType">排序类型 desc或者asc</param>

        /// /// <param name="PageSize">页面大小</param>

        /// <param name="PageIndex">页索引</param>

        /// <param name="strWhere">条件</param>

        /// <returns></returns>

        public DataSet GetList(string tblName,string strGetFields, string strOrder, string strOrderType, int PageSize, 
int PageIndex, string strWhere)

        {

            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetStoredProcCommand("proc_FenYe");

            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString,tblName);

            db.AddInParameter(dbCommand, "strGetFields", DbType.AnsiString, strGetFields);

            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);

            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);

            db.AddInParameter(dbCommand, "strOrder ", DbType.String, strOrder);

            db.AddInParameter(dbCommand, "strOrderType", DbType.String, strOrderType);

            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);

            return db.ExecuteDataSet(dbCommand);

        }

 

调用方式

//绑定循环

        private void BindRep()

        {

            repPro.DataSource = prodao.GetList("shop_category a  inner join shop_product b on a.id = b.caid", 
" b.id,proname,marketprice,memberprice,vipprice,caname", "b.createdate", "desc", anp.PageSize, anp.CurrentPageIndex, GetCond());

          

            repPro.DataBind();

        }

 

//保存或者是修改

        protected void btnSave_Click(object sender, EventArgs e)

        {

            string proname = txtProname.Text.Trim();

            string procate = ddlCate.SelectedValue;

            string proimg = img.ToolTip;

            string marketprice = txtMarket.Text.Trim();

            string memberprice = txtMember.Text.Trim();

            string vipprice = txtVip.Text.Trim();

            string prostate = txtState.Text.Trim();

            string guige = txtGg.Text.Trim();

            string danwe = txtDw.Text.Trim();

            //如何获取CheckBoxList 的值呢?实际上CheckBoxList生成的html是多个id和name都不同的checkbox



            int tejia = chk.Items[0].Selected ? 1 : 0; //如果第一项被选择,那么赋值为1,否则即使没有选择,也赋值为0            

            int xinp = chk.Items[1].Selected ? 1 : 0;

            int vip = chk.Items[2].Selected ? 1 : 0;



            string desc = txtDesc.Text.Trim();

            

            //开始验证

            if (proname.Length==0 || marketprice.Length==0 || memberprice.Length==0 || vipprice.Length==0 )

            {

                Niunan.Shop.Utility.Tool.alert("商品名称,价格不能为空", this.Page);

                return;

            }

            //验证价格

            decimal a, b, c;

            if (!decimal.TryParse(marketprice,out a) ||!decimal.TryParse(memberprice,out b) ||!decimal.TryParse(vipprice,out c))

            {

                Utility.Tool.alert("价格必须是数字", this.Page);

                return;

            }

           



            Model.Product pt = new Model.Product {

                proname = proname,

                caid = int.Parse(procate),

                proimg = proimg,

                marketprice = decimal.Parse(marketprice),

                memberprice = decimal.Parse(memberprice),

                vipprice = decimal.Parse(vipprice),

                state = prostate,

                guige = guige,

                danwei = danwe,

                istj = tejia,

                isvip = vip,

                isxp = xinp,

                prodesc = desc

            };

            //我们先判断,这是不是修改,如果是修改商品,我们就直接修改,后面的增加,我们就不执行了

            string id = Request.QueryString["id"];

            int x;

            if (int.TryParse(id, out x))

            {

                pt.id = x;

                pd.Update(pt);

                Niunan.Shop.Utility.Tool.alert("修改成功", "product_list.aspx", this.Page);

                return;

            }





            //这里是添加商品

            pd.Add(pt);

            Niunan.Shop.Utility.Tool.alert("保存成功", Request.Url.ToString(), this.Page);

            



        }



        //图片上传保存

        protected void btnUpload_Click(object sender, EventArgs e)

        {

            try

            {

                string[] houzhui = { ".jpg", ".png", ".jpeg", ".bmp" };

                string savaPath = Server.MapPath("~/upload/image/");

                string proimg=Niunan.Shop.Utility.Tool.Upload(fileImg, houzhui, 2, savaPath);

                img.ImageUrl = "~/upload/image/" + proimg;

                img.ToolTip = proimg;//这个值,是最后我们保存到数据库的值,但是没有加 upload/image ,我们在前台显示要手动加

            }

            catch (Exception ex)

            {



                Niunan.Shop.Utility.Tool.alert(ex.Message, this.Page);

            }

        }
 
/// <summary>上传文件方法

        /// 

        /// </summary>

        /// <param name="myFileUpload">上传控件ID</param>

        /// <param name="allowExtensions">允许上传的扩展文件名类型,如:string[] allowExtensions = 
{ ".doc", ".xls", ".ppt", ".jpg", ".gif" };</param>

        /// <param name="maxLength">允许上传的最大大小,以M为单位</param>

        /// <param name="savePath">保存文件的目录,注意是绝对路径,如:Server.MapPath("~/upload/");</param>

        /// <param name="saveName">保存的文件名,如果是""则以原文件名保存</param>

        public static string Upload(FileUpload myFileUpload, string[] allowExtensions, int maxLength, string savePath)

        {

            // 文件格式是否允许上传

            bool fileAllow = false;





            //检查是否有文件案

            if (myFileUpload.HasFile)

            {

                // 检查文件大小, ContentLength获取的是字节,转成M的时候要除以2次1024

                if (myFileUpload.PostedFile.ContentLength / 1024 / 1024 >= maxLength)

                {

                    throw new Exception("只能上传小于" + maxLength + "M的文件!");

                }





                //取得上传文件之扩展文件名,并转换成小写字母

                string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();

                string tmp = "";   // 存储允许上传的文件后缀名

                //检查扩展文件名是否符合限定类型

                for (int i = 0; i < allowExtensions.Length; i++)

                {

                    tmp += i == allowExtensions.Length - 1 ? allowExtensions[i] : allowExtensions[i] + ",";

                    if (fileExtension == allowExtensions[i])

                    {

                        fileAllow = true;

                    }

                }





                if (fileAllow)

                {

                    try

                    {

                        string datedir = DateTime.Now.ToString("yyyyMMdd");

                        if (!Directory.Exists(savePath + datedir))

                        {

                            Directory.CreateDirectory(savePath + datedir);

                        }

                        string saveName = Guid.NewGuid() + fileExtension;//这里是使用 guid的值来保存为图片的名字

                        string path = savePath + datedir + "/" + saveName;



                        //存储文件到文件夹

                        myFileUpload.SaveAs(path);

                        return datedir + "/" + saveName;

                    }

                    catch (Exception ex)

                    {

                        throw new Exception(ex.Message);

                    }

                }

                else

                {

                    throw new Exception("文件格式不符,可以上传的文件格式为:" + tmp);

                }

            }

            else

            {

                throw new Exception("请选择要上传的文件!");

            }

        }

你可能感兴趣的:(产品)