C#-经典购物车流程全代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

C#购物车及后台代码C#程序语言一.防止SQL注入publicstaticboolSqlFilter2(stringInText){stringword=and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join|';if(InText==null)returnfalse;foreach(stringstr_tinword.Split('|')){if((InText.ToLower().IndexOf(str_t+)-1)||(InText.ToLower().IndexOf(+str_t)-1)||(InText.ToLower().IndexOf(str_t)-1)){returntrue;//返回有}}}二.MD5加密usingsystem.web.securitystringpwd=FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text,MD5);四..配置文件的加密与解密数据库连接字符串appSettingsaddkey=”ConnectionString”value=”server=(Local);database=test;pwd=sa;uid=sa;”//appSettings加密Configurationconfig=WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);ConfigurationSectionsectionconfig.GetSection(”appSettings”);if(section!=null&&!section.SectionInformation.IsProtected){Section.SectionInformation.ProtectSection(”RsaProtectedConfigurationProvider”);Config.Save();}解密Configurationconfig=WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);ConfigurationSectionsection=config.GetSection(“appSettings”);If(section!=null&&secion.SectionInformation.IsProtected){SectionSectionInformation.UnprotectSection();Config.Save();}五.邮件的发送和接收Encodingencoding=Encoding.GetEncoding(GB2312);stringaddress=TextBox1.Text.Trim();stringbiaoti=购物网用户激活;stringcontent=status.aspx?id=+TextBox2.Text+;MailAddressfrom=newMailAddress(clear_rm@163.com,Fei_L,encoding);MailAddressto=newMailAddress(address);MailMessagemail=newMailMessage(from,to);mail.Subject=biaoti;mail.Body=content;mail.SubjectEncoding=encoding;mail.BodyEncoding=encoding;SmtpClientsmtp=newSmtpClient(smtp.163.com);smtp.DeliveryMethod=SmtpDeliveryMethod.Network;smtp.UseDefaultCredentials=true;smtp.Credentials=newSystem.Net.NetworkCredential(clear_rm@163.com,061110);smtp.Send(mail);dbcon.message(会员注册成功,请登录邮件激活会员!);六.产品添加protectedvoidButton1_Click(objectsender,EventArgse){SqlConnectionconn=dbcon.conn();conn.Open();SqlCommandcmd_rz=newSqlCommand(selectcount(*)fromshopwheresortid='+TextBox6.Text+',conn);intnum_rz=(int)cmd_rz.ExecuteScalar();conn.Close();if(num_rz0){dbcon.message(此产品编号已登记!);}else{if(DropDownList1.SelectedItem.Text==产品分类){dbcon.message(请选择分类!);}else{conn.Open();SqlCommandcmd=newSqlCommand(insertintoshop(sortid,sortname,product,,buy,inventory,discount,price,path,parentpath)values('+TextBox6.Text+','+TextBox1.Text+','+FCKeditor1.Value+','+TextBox3.Text+','+TextBox3.Text+','+TextBox4.Text+','+(int.Parse(TextBox5.Text)*int.Parse(TextBox4.Text)).ToString()+','+Session[picid].ToString()+','+DropDownList1.SelectedItem.Text+'),conn);intnum=cmd.ExecuteNonQuery();conn.Close();if(num0)dbcon.message(添加产品成功!);}}}七.用户登录状态的保存if(Session[uid]!=null){stringuid=;||if(Request[id].ToString()!=){uid=Request[id].ToString();}if(!Page.IsPostBack){uid=username.Text;SqlDataReaderrd=select(uid);}}八.购物车a=e.CommandArgument.ToString();//if(Session[ID]!=null)//如果用户没有登录//{if(Session[Cart]==null)//如果购物篮不存在{//Response.Write(scriptlanguage='javascript'alert('1')/script);this.BuildCart();//创建购物篮并将商品存入}else//购物篮存在{DataTablecart=Session[Cart]asDataTable;if(this.ExistBook(cart))//如果购物篮已存在该商品{this.BuildSession(cart);//修改购物篮中的商品}}//Response.Redirect(buycar.aspx);//跳转到购物车界面//}//else//{//如果没有登录,跳转到登录界面//Response.Redirect(denglu.aspx);//}}publicboolExistBook(DataTablecart){foreach(DataRowdrincart.Rows){if(dr[QID].ToString()==a){dr[NUM]=Convert.ToInt32(dr[NUM])+1;Session[Cart]=cart;//Response.Redirect(Cart.aspx);}}returntrue;}publicvoidBuildCart(){//创建购物车DataTablecart=newDataTable();//已经创建了表,但是没有字段cart.Columns.Add(QID);cart.Columns.Add(ID);cart.Columns.Add(NUM);cart.Columns.Add(IID);cart.Columns.Add(name);cart.Columns.Add(jiage);//Response.Write(scriptlanguage='javascript'alert('创建了表')/script);//cart表中已有5个字段//将点击的商品加入购物车中this.BuildSession(cart);//往cart中添加一条记录}/////summary/////添加新书//////summary/////paramname=cart/parampublicvoidBuildSession(DataTablecart){//新建一个数据行的记录DataRowdr=cart.NewRow();SqlDataReaderrd=select(a);if(rd.Read()){dr[QID]=a;//存商品编号dr[ID]=rd[1];//存商品名称dr[NUM]=1;//存商品数量dr[iid]=rd[2];dr[name]=rd[3];//存入单价dr[jiage]=rd[4];//存入商品图片地址cart.Rows.Add(dr);//将数据行加入到cart表中//Response.Write(scriptlanguage='javascript'alert('添加了记录')/script);//Response.Write(rd[1]);//Response.Write(rd[2]);//Response.Write(rd[3]);//Response.Write(rd[4]);}Session[Cart]=cart;//将数据表cart的值存入session保存//Response.Write(scriptlanguage='javascript'alert('购买成功')/script);RegisterStartupScript(,scriptlanguage='javascript'alert('购买成功')/script);}九..计算总价if(Session[cart]==null){Response.Write(scriptalert('购物车为空,请挑选商品');window.location.href='shengri.aspx?id=1';/script);}DataTablecart=(DataTable)Session[cart];GridView1.DataSource=cart;GridView1.DataBind();intNUM=0;foreach(DataRowdrincart.Rows){NUM+=int.Parse(dr[NUM].ToString())*int.Parse(dr[jiage].ToString());}Label3.Text=NUM.ToString();十.删除时跳出对话框双击GridView的OnRowDataBound事件;protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse){if(e.Row.RowType==DataControlRowType.DataRow){if(e.Row.RowState==DataControl

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功