Asp.net+Sql server 购物车绑定
马上要毕业了,回想起大学四年里学的东西太少了.不完全因为学校牌子不怎么亮的事,主要是本
人的原因,得赶紧学点东西吧!不为别的就给自的面 学吧! 自己学Asp.Net快一年了,从来没做过大点
的程序.选毕业设计一口气硬着头选择了一个电子商务网站的开发与实现.开始写代码的时候没感觉到难,
但是开始写购物车这个模块的时候问题就出现了.也许是第一次做这个例子的原因吧,有些东西真的不
知道怎么去写,只好上玩搜索来搜索去找了好多的代码,拿回去一看差点没哭出来,苦苦下的代码一个一
样,没有两个是一样的,没办法一个一个去运行,哭^^^
还是自己作吧^^^^^自己动手丰衣足食不是吗?我刚开始用Session变量来接受选购的物品信息
然后在把它存到Hashtable表中了,但是本人势力实在是无法完成这个想法了,虽然知道这样做可以跳过
对数据库的大量操作可以提高系统的性能,但是没办法只好选择最笨的办法了^^^访问数据库的方法
来完整这个小小的例子了<要是高手就不用看了,会笑掉你们的牙的,发布的目的就是给象我这样初次
这个例子的雏鸟做一个参考资料吧了!
按例还是从创建数据库开始讲吧!
数据库创建代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(
BID varchar(20) primary key,
Bname varchar(200) not null,
Bzuozhe varchar(200) not null,
Bchubanshe varchar(200) not null,
Bmoney varchar(50) not null,
Bnumber int default 0,
Bpic varchar(100) not null,
Bsum varchar(50) not null
)这个是创建的数据信息表用来存放购物车信息的
数据库创建完了 接下来完成售板块吧!先看一下已经作完的效果图:
![小拖图片](https://www.cnblogs.com/images/cnblogs_com/yelang/Boox.jpg)
看完图片马上回到正文上^^^^我们现在用datalist来完成,首先在在项目中拽
进一个datalist控件.右键点选择编辑摸板项,也可以直接在html代码中实现
实现的代码片段如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这里没什么奇怪的为了页面美观在datalist中加了一个table美化了界面,绑定完后
就回到数据显示模块
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
绑定工作完事 以上的主要就是从书库中读取数据显示在浏览器里 接下我们来完成购
买物品的过程(购物车)项目
首先,我们在datalist中拽入一个linkbutton 然后把它的commandname属性设成
select说明选择这一行项 然后在datalist的事件中选择ItemCommand事件进行数据
绑定(开始购物)代码如下:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=I_shop;uid=sa;pwd=sa;");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
string BooxID =this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
if (e.CommandName == "select")
{
cmd.CommandText = "select * from Boox where Bid='"+BooxID+"'";
SqlDataReader drs = cmd.ExecuteReader();
drs.Read();
string Booxname=drs["Bname"].ToString();//把购买的物品信息读出来
string Bzuozhe=drs["Bzuozhe"].ToString();
string Bchubanshe=drs["Bchubanshe"].ToString();
string Bmoney=drs["Bmoney"].ToString();
string Bpic=drs["Bpic"].ToString();
drs.Close();
cmd.CommandText = "select * from BusBoox where BID='" + BooxID + "' ";//在去已购的物品表信息表去查找所购买的物品信息
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())//如果要是找到此物品信息就说明以前成功购买过次商品
{
int BooxNumber = Convert.ToInt32(dr["Bnumber"])+1;//原来的数目上加1
double sum = BooxNumber * Convert.ToDouble(Bmoney);
cmd.CommandText = "update BusBoox set Bnumber='" + BooxNumber + "',Bsum='"+sum.ToString()+"' where BID='" + BooxID + "'";
}
else//如果找不到物品任何记录就在已购的物品信息表中插入一条新的信息
{
cmd.CommandText = "insert into BusBoox values('" + BooxID + "','" + Booxname + "','" + Bzuozhe + "','" + Bchubanshe + "','" + Bmoney + "',1,'" + Bpic + "','" + Bmoney + "')";
}
dr.Close();
cmd.ExecuteNonQuery();
}
}
{
SqlConnection con = new SqlConnection("server=.;database=I_shop;uid=sa;pwd=sa;");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
string BooxID =this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
if (e.CommandName == "select")
{
cmd.CommandText = "select * from Boox where Bid='"+BooxID+"'";
SqlDataReader drs = cmd.ExecuteReader();
drs.Read();
string Booxname=drs["Bname"].ToString();//把购买的物品信息读出来
string Bzuozhe=drs["Bzuozhe"].ToString();
string Bchubanshe=drs["Bchubanshe"].ToString();
string Bmoney=drs["Bmoney"].ToString();
string Bpic=drs["Bpic"].ToString();
drs.Close();
cmd.CommandText = "select * from BusBoox where BID='" + BooxID + "' ";//在去已购的物品表信息表去查找所购买的物品信息
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())//如果要是找到此物品信息就说明以前成功购买过次商品
{
int BooxNumber = Convert.ToInt32(dr["Bnumber"])+1;//原来的数目上加1
double sum = BooxNumber * Convert.ToDouble(Bmoney);
cmd.CommandText = "update BusBoox set Bnumber='" + BooxNumber + "',Bsum='"+sum.ToString()+"' where BID='" + BooxID + "'";
}
else//如果找不到物品任何记录就在已购的物品信息表中插入一条新的信息
{
cmd.CommandText = "insert into BusBoox values('" + BooxID + "','" + Booxname + "','" + Bzuozhe + "','" + Bchubanshe + "','" + Bmoney + "',1,'" + Bpic + "','" + Bmoney + "')";
}
dr.Close();
cmd.ExecuteNonQuery();
}
}
购物完成了后就看看自己都购买了些 已经消费了多少呢 往下看代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
通过以上的代码跳转查看购物车页面 这里我直接给出他代码和作完的效果图:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
double sum = 0; double number;
SqlConnection con = new SqlConnection("server=.;database=I_shop;uid=sa;pwd=sa;");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from BusBoox";
SqlDataReader dr = cmd.ExecuteReader();
this.DataList1.DataSource = dr;
this.DataList1.DataBind();
dr.Close();
cmd.CommandText = "select Bnumber,Bmoney from BusBoox";
SqlDataReader drs = cmd.ExecuteReader();
while (drs.Read())
{
number = Convert.ToInt32(drs["Bnumber"])* Convert.ToDouble(drs["Bmoney"]);
sum += number;
}
Response.Write(sum);
con.Close();
drs.Close();
}
}
{
if (!IsPostBack)
{
double sum = 0; double number;
SqlConnection con = new SqlConnection("server=.;database=I_shop;uid=sa;pwd=sa;");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from BusBoox";
SqlDataReader dr = cmd.ExecuteReader();
this.DataList1.DataSource = dr;
this.DataList1.DataBind();
dr.Close();
cmd.CommandText = "select Bnumber,Bmoney from BusBoox";
SqlDataReader drs = cmd.ExecuteReader();
while (drs.Read())
{
number = Convert.ToInt32(drs["Bnumber"])* Convert.ToDouble(drs["Bmoney"]);
sum += number;
}
Response.Write(sum);
con.Close();
drs.Close();
}
}
前台绑定代码如下:
<asp:DataList ID="DataList1" runat="server" Font-Size="9pt">
<HeaderTemplate>
<table>
<tr>
<td style="width:40px">删除</td>
<td style="width:200px">商品名称</td>
<td style="width:60px">单价</td>
<td style="width:80px">数量</td>
<td style="width:60px">小计</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:40px">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete">删除</asp:LinkButton></td>
<td style="width:200px"><%#DataBinder.Eval(Container.DataItem,"Bname") %></td>
<td style="width:60px"><%#DataBinder.Eval(Container.DataItem,"Bmoney") %></td>
<td style="width:80px">
<asp:TextBox ID="TextBox1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Bnumber") %>' AutoPostBack="false"></asp:TextBox></td>
<td style="width:80px">
<%#DataBinder.Eval(Container.DataItem, "Bsum","{0:C}")%>(元)</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td style="width:40px">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete">删除</asp:LinkButton></td>
<td style="width:200px"><%#DataBinder.Eval(Container.DataItem,"Bname")%></td>
<td style="width:60px"><%#DataBinder.Eval(Container.DataItem,"Bmoney") %></td>
<td style="width:80px">
<asp:TextBox ID="TextBox1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Bnumber") %>'></asp:TextBox></td>
<td style="width:80px">
<%#DataBinder.Eval(Container.DataItem, "Bsum", "{0:C}")%>(元)</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
<HeaderTemplate>
<table>
<tr>
<td style="width:40px">删除</td>
<td style="width:200px">商品名称</td>
<td style="width:60px">单价</td>
<td style="width:80px">数量</td>
<td style="width:60px">小计</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:40px">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete">删除</asp:LinkButton></td>
<td style="width:200px"><%#DataBinder.Eval(Container.DataItem,"Bname") %></td>
<td style="width:60px"><%#DataBinder.Eval(Container.DataItem,"Bmoney") %></td>
<td style="width:80px">
<asp:TextBox ID="TextBox1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Bnumber") %>' AutoPostBack="false"></asp:TextBox></td>
<td style="width:80px">
<%#DataBinder.Eval(Container.DataItem, "Bsum","{0:C}")%>(元)</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td style="width:40px">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete">删除</asp:LinkButton></td>
<td style="width:200px"><%#DataBinder.Eval(Container.DataItem,"Bname")%></td>
<td style="width:60px"><%#DataBinder.Eval(Container.DataItem,"Bmoney") %></td>
<td style="width:80px">
<asp:TextBox ID="TextBox1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Bnumber") %>'></asp:TextBox></td>
<td style="width:80px">
<%#DataBinder.Eval(Container.DataItem, "Bsum", "{0:C}")%>(元)</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
绑定完效果如下:
![效果图片](https://www.cnblogs.com/images/cnblogs_com/yelang/Bus.jpg)
好了到这快我想给你提醒都已经完事了 接下来就看你们怎么改造这个了!
posted on 2007-04-11 22:02 男人的世界 让男人来闯 阅读(1163) 评论(2) 编辑 收藏 举报