现在的位置: 首页 > 综合 > 正文

asp.net购物车实现——-结合数据库的方式

2013年10月30日 ⁄ 综合 ⁄ 共 10857字 ⁄ 字号 评论关闭

 结合数据库的购物车我没有考虑用户是否登陆,默认必须登陆。

实际可以看出用session和数据库的购物车实现是大同小异的。

操作数据库是我用的是存储过程。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BrowserGoods.aspx.cs" Inherits="BrowserGoods" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body bgcolor="#ffffcc">
    <form id="form1" runat="server">
        <div>
            <table width="100%">
                <tr>
                    <td align="left">
                        <asp:Label ID="Label2" runat="server" ForeColor="Red" Style="font-weight: bold" Text="当前用户是:"></asp:Label>
                        <asp:Label ID="Label7" runat="server" ForeColor="Red" Height="20px" Style="font-weight: bold"
                            Width="100px"></asp:Label></td>
                </tr>
                <tr>
                    <td align="center">
                        <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="隶书" Font-Size="X-Large"
                            ForeColor="Red" Text="商品信息浏览表"></asp:Label></td>
                </tr>
                <tr>
                    <td align="center">
                        <asp:DataList ID="DataList1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"
                            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" GridLines="Both"
                            DataKeyField="GoodsID" Width="100%" OnItemCommand="DataList1_ItemCommand" OnSelectedIndexChanged="DataList1_SelectedIndexChanged">
                            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                            <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                            <ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                            <ItemTemplate>
                                <table width="100%">
                                    <tr>
                                        <td align="left" rowspan="4">
                                            <asp:Image ID="Image1" runat="server" ImageUrl='<%#DataBinder.Eval(Container.DataItem,"GoodsPhoto")%>' /></td>
                                        <td align="left">
                                            <asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Red"
                                                Text="商品名:"></asp:Label></td>
                                        <td align="left">
                                            <asp:Label ID="Label5" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"GoodsName")%>'></asp:Label></td>
                                        <td align="left">
                                            <asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Red"
                                                Text="商品类型:"></asp:Label></td>
                                        <td align="left">
                                            <asp:Label ID="Label6" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"GoodsIntroduce") %>'></asp:Label></td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <asp:Label ID="Label4" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Red"
                                                Text="商品单价:"></asp:Label></td>
                                        <td align="left">
                                            <asp:Label ID="Label8" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"GoodsPrice")%>'></asp:Label></td>
                                        <%--<td align="left" >
                                        <asp:Label ID="Label7" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Red"
                                            Text="商品数量:"></asp:Label></td>
                                    <td align=left >
                                        <asp:Label ID="Label9" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"GoodsQuantity")%>'></asp:Label></td>--%>
                                    </tr>
                                    <tr>
                                        <td align="left" colspan="4" rowspan="2">
                                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="add" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"GoodsName")+"|"+DataBinder.Eval(Container.DataItem,"GoodsIntroduce") %>'
                                                Font-Bold="False" Font-Names="隶书">加入购物车</asp:LinkButton>
                                            <asp:LinkButton ID="LinkButton2" runat="server" CommandName="see" Font-Names="隶书"
                                                OnClick="LinkButton2_Click">查看购物车</asp:LinkButton></td>
                                    </tr>
                                    <tr>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:DataList></td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

 protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {

        if (e.CommandName.ToLower() == "add")
        {
            //添加到购物车中,购物车中的商品数据保存在表OrderInfo中
            SaveSubGoodsClass Goods = null;
            Goods = GetSubGoodsInformation(e);
            string UserIP = Convert.ToString(Request.UserHostAddress);
            int ExistsNum = ExistsInfo(Goods.GoodsID, UserIP);
            if (ExistsNum == -100)
            {
                //当购物车没有任何信息时

                if (Goods == null)
                {
                    //显示错误信息
                    Response.Write("<script>alert('没有可用的数据')<script>");
                    return;

                }
                //添加商品信息到表OrderInfo中

                AddOrderInfo(Goods.GoodsID, 1, DateTime.Now, UserIP);
                Response.Write("<script>alert('恭喜您,添加成功!')</script>");
            }
            else
            {
                //当购物车中已经存在商品时,获取以前的商品信息

                if (Goods == null)
                {
                    //显示错误信息
                    Response.Write("<script>alert('没有可用的数据')<script>");
                    return;
                }
                //判断是否已经添加了该商品,如果已添加该商品,则修改该商品的数量;否则添加新的商品
                if (ExistsNum == 200)
                {
                    InsertOrUpdateInfo(200, Goods.GoodsID, Goods.OrderDate, UserIP);
                }
                else
                {
                    if (ExistsNum == -200)
                    {
                        AddOrderInfo(Goods.GoodsID, 1, DateTime.Now, UserIP);

                    }
                }
                Response.Write("<script>alert('恭喜您,添加成功!')</script>");
            }

        }
        if (e.CommandName.ToLower() == "see")
        {
            //向目标页面传递数据
            string sUrl = "CommitGoods.aspx?id=" + Label7.Text;
            Response.Redirect(sUrl);

        }
    }
    //当购买商品时,获取商品信息
    public SaveSubGoodsClass GetSubGoodsInformation(DataListCommandEventArgs e)
    {
        //获取购物车中的信息
        SaveSubGoodsClass Goods = new SaveSubGoodsClass();
        Goods.GoodsID = int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
        Goods.OrderDate = DateTime.Now;
        //获取商品价格
        Label PriceLable = (Label)e.Item.FindControl("Label8");
        if (PriceLable == null)
        {
            //显示错误信息
            Response.Write("<script>alert('没有可用的数据')<script>");
            return null;
        }
        Goods.GoodsPrice = decimal.Parse(PriceLable.Text.Trim());
        string GoodsStyle = e.CommandArgument.ToString();
        int index = GoodsStyle.IndexOf("|");
        if (index < -1 || index + 1 >= GoodsStyle.Length)
            return Goods;
        Goods.GoodsName = GoodsStyle.Substring(0, index);
        Goods.GoodsIntroduce = GoodsStyle.Substring(index + 1);
        Goods.GoodsName = ((Label)e.Item.FindControl("Label5")).Text;
        Goods.GoodsIntroduce = ((Label)e.Item.FindControl("Label6")).Text; ;
        return (Goods);

    }
    //向购物车中添加商品信息
    public void AddOrderInfo(int GoodsID, int GoodsNum, DateTime OrderDate, string UserIP)
    {
        SqlCommand cmd = new SqlCommand("InsertOrderInfo", myConn);
        cmd.CommandType = CommandType.StoredProcedure;
        //添加参数
        SqlParameter goodsID = new SqlParameter("@GoodsID", SqlDbType.Int, 4);
        goodsID.Value = GoodsID;
        cmd.Parameters.Add(goodsID);
        SqlParameter goodsNum = new SqlParameter("@GoodsNum", SqlDbType.Int, 4);
        goodsNum.Value = GoodsNum;
        cmd.Parameters.Add(goodsNum);
        SqlParameter orderDate = new SqlParameter("@OrderDate", SqlDbType.DateTime, 8);
        orderDate.Value = OrderDate;
        cmd.Parameters.Add(orderDate);
        SqlParameter userIP = new SqlParameter("@UserIP", SqlDbType.VarChar, 50);
        userIP.Value = UserIP;
        cmd.Parameters.Add(userIP);

        //执行存储过程
        myConn.Open();
        try
        {
            cmd.ExecuteNonQuery();

        }
        catch (Exception)
        {
            Response.Write("操作失败");
          
        }

        cmd.Dispose();
        myConn.Close();

    }
    //判断是否已经添加了该商品,如果已添加该商品,则修改该商品的数量;否则添加新的商品
    public void InsertOrUpdateInfo(int ReturnValue, int GoodsID,  DateTime OrderDate, string UserIP)
    {

        SqlCommand cmd = new SqlCommand("InsertOrUpdateOrderInfo", myConn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter returnValue = new SqlParameter("@ReturnValue", SqlDbType.Int, 4);
        returnValue.Value = ReturnValue;
        cmd.Parameters.Add(returnValue);
        SqlParameter goodsID = new SqlParameter("@GoodsID", SqlDbType.Int, 4);
        goodsID.Value = GoodsID;
        cmd.Parameters.Add(goodsID);
        SqlParameter orderDate = new SqlParameter("@OrderDate", SqlDbType.DateTime, 8);
        orderDate.Value = OrderDate;
        cmd.Parameters.Add(orderDate);
        SqlParameter userIP = new SqlParameter("@UserIP", SqlDbType.VarChar, 50);
        userIP.Value = UserIP;
        cmd.Parameters.Add(userIP);
        //执行存储过程
        myConn.Open();
        try
        {

            cmd.ExecuteNonQuery();

        }
        catch (Exception)
        {
            Response.Write("操作失败");

        }
        cmd.Dispose();
        myConn.Close();
    }
    public int ExistsInfo(int GoodsID, string UserIP)
    {
        SqlCommand cmd = new SqlCommand("ExistsInfo", myConn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter goodsID = new SqlParameter("@GoodsID", SqlDbType.Int, 4);
        goodsID.Value = GoodsID;
        cmd.Parameters.Add(goodsID);
        SqlParameter userIP = new SqlParameter("@UserIP", SqlDbType.VarChar, 50);
        userIP.Value = UserIP;
        cmd.Parameters.Add(userIP);
        cmd.Parameters.Add("returnValue", SqlDbType.Int);
        cmd.Parameters["returnValue"].Direction = ParameterDirection.ReturnValue;
      

        //执行存储过程
        myConn.Open();
        try
        {
            cmd.ExecuteNonQuery();
         
        }
        catch (Exception)
        {
            Response.Write("操作失败");
         
        }
     
        cmd.Dispose();
        myConn.Close();
        return int.Parse(cmd.Parameters["returnValue"].Value.ToString());

    }

抱歉!评论已关闭.