Exemple #1
0
    private Object bLock = new Object();//同步变量
    protected void btnSaveOrder_Click(object sender, EventArgs e)
    {
        String orderid;                                                                                     //保存订单编号

        lock (bLock)                                                                                        //同步代码,确保不同用户不会生成相同的编号
        {
            orderid = System.DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString(); //用时间戳生成订单编号
        }
        using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
        {
            conn.Open();
            SqlTransaction tran         = conn.BeginTransaction();
            ArrayList      shopCartList = (ArrayList)Session["shopList"];                                     //得到购物车类别
            string         sql          = "insert into orderPocduct(orderid,ISBN,count,price,sumPrice) ";
            sql += "values(@orderid,@isbn,@count,@price,@sumPrice)";                                          //订单保存语句
            string     bSql = "update bookInfo set bCount=bCount-1,bSaleCount=bSaleCount+1 where ISBN=@isbn"; //修改图书库存语句
            SqlCommand cmd  = new SqlCommand(sql, conn, tran);
            SqlCommand bCom = new SqlCommand(bSql, conn, tran);
            try
            {
                bool   bSuccess = true;
                double sumPrice = 0;
                //////////////////////////////////////
                //  保存订单编号的所有商品信息      //
                //////////////////////////////////////
                for (int i = 0; i < shopCartList.Count; i++)
                {
                    shopObject shop = (shopObject)shopCartList[i];
                    sumPrice += shop.Bprice * shop.Count;
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@orderid", SqlDbType.Char).Value   = orderid;
                    cmd.Parameters.Add("@isbn", SqlDbType.VarChar).Value   = shop.Isbn;
                    cmd.Parameters.Add("@count", SqlDbType.Int).Value      = shop.Count;
                    cmd.Parameters.Add("@price", SqlDbType.Float).Value    = shop.Bprice;
                    cmd.Parameters.Add("@sumPrice", SqlDbType.Float).Value = shop.Bprice * shop.Count;

                    bCom.Parameters.Clear();
                    bCom.Parameters.Add("@isbn", SqlDbType.VarChar).Value = shop.Isbn;

                    if (cmd.ExecuteNonQuery() <= 0)
                    {
                        bSuccess = false;
                    }
                    else if (bCom.ExecuteNonQuery() <= 0)
                    {
                        bSuccess = false;
                    }

                    if (!bSuccess)
                    {
                        break;
                    }
                }
                if (!bSuccess)//如果一个商品保存失败,撤销订单保存
                {
                    tran.Rollback();
                    Response.Write("系统错误请稍候提交!");
                }
                else
                {
                    //////////////////////////////
                    //  保存订单信息到数据库   //
                    ////////////////////////////
                    sql             = "insert into orders (orderid,postAdress,postNumber,orderMember,pid,orderPrice,isPay,isPost,findPassword) ";
                    sql            += "values(@orderid,@postAdress,@postNumber,@orderMember,@pid,@orderPrice,@isPay,@isPost,@findPassword)";
                    cmd.CommandText = sql;
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@orderid", SqlDbType.Char).Value         = orderid;
                    cmd.Parameters.Add("@postAdress", SqlDbType.VarChar).Value   = txtPostAdress.Text;
                    cmd.Parameters.Add("@postNumber", SqlDbType.Char).Value      = txtPostNumber.Text;
                    cmd.Parameters.Add("@orderMember", SqlDbType.VarChar).Value  = txtOrderMember.Text;
                    cmd.Parameters.Add("@pid", SqlDbType.Int).Value              = ddlPid.SelectedValue;
                    cmd.Parameters.Add("@orderPrice", SqlDbType.Float).Value     = sumPrice;
                    cmd.Parameters.Add("@isPay", SqlDbType.Bit).Value            = 0;
                    cmd.Parameters.Add("@isPost", SqlDbType.Bit).Value           = 0;
                    cmd.Parameters.Add("@findPassword", SqlDbType.VarChar).Value = txtFindPassword.Text;
                    if (cmd.ExecuteNonQuery() > 0)//保存成功调整到成功页面
                    {
                        tran.Commit();
                        Session.Clear();
                        Response.Redirect("viewSuccessOrder.aspx?orderId=" + orderid);
                    }
                    else//保存出错撤销事物
                    {
                        tran.Rollback();
                        Response.Write("系统错误请稍候提交!");
                    }
                }
            }
            catch (Exception ex)//保存出错撤销事物
            {
                Response.Write("系统错误请稍候提交!");
                tran.Rollback();
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
    }
Exemple #2
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)                                             //判断是否为页面回发事件
        {
            string op = Request.Params["op"];                        //接收操作参数
            if (op != null && op.Equals("add"))                      //如果为添加商品事件,执行图书添加操作
            {
                string    bid      = Request.Params["bid"];          //接收参数传递的图书编号
                ArrayList shopList = (ArrayList)Session["shopList"]; //得到保存在session的购物车列表
                if (shopList == null)                                //如果是第一次使用购物车,新建一个购物车对象
                {
                    shopList = new ArrayList();
                }
                //查询购物车内是否存在该商品
                int i;
                for (i = 0; i < shopList.Count; i++)
                {
                    if (bid == ((shopObject)shopList[i]).Isbn)
                    {
                        ((shopObject)shopList[i]).Count++;//如果找到增加图书数量
                        break;
                    }
                }
                //如果没有找到,折添加商品
                if (i == shopList.Count)
                {
                    shopObject shop = new shopObject();
                    shop.Isbn  = bid;
                    shop.Count = 1;
                    //查询出图书名称
                    string        sql   = "select bname,bprice from bookInfo where ISBN='" + bid + "'";
                    SqlDataReader dread = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sql);
                    if (dread.Read())
                    {
                        shop.Bname  = dread.GetString(0);
                        shop.Bprice = dread.GetDouble(1);
                    }
                    shopList.Add(shop);
                }
                Session["shopList"] = shopList;                      //保存购物车
            }
            string isbn = Request.Params["isbn"];                    //接收参数传递的图书编号
            if (op != null && op.Equals("subcount"))                 //购物车物品数量减少事件处理
            {
                ArrayList shopList = (ArrayList)Session["shopList"]; //得到保存在session的购物车列表
                int       i;
                for (i = 0; i < shopList.Count; i++)
                {
                    if (isbn == ((shopObject)shopList[i]).Isbn)
                    {
                        ((shopObject)shopList[i]).Count--;        //如果找到增加图书数量
                        if (((shopObject)shopList[i]).Count == 0) //如果图书数量为0删除图书
                        {
                            shopList.RemoveAt(i);
                        }
                        break;
                    }
                }
            }
            if (op != null && op.Equals("addcount"))                 //购物车物品数量增加事件处理
            {
                ArrayList shopList = (ArrayList)Session["shopList"]; //得到保存在session的购物车列表
                int       i;
                for (i = 0; i < shopList.Count; i++)
                {
                    if (isbn == ((shopObject)shopList[i]).Isbn)
                    {
                        ((shopObject)shopList[i]).Count++;//如果找到增加图书数量
                        break;
                    }
                }
            }

            //显示购物车
            ArrayList shopCartList = (ArrayList)Session["shopList"];
            GridView1.DataSource = shopCartList;
            GridView1.DataBind();
            GridView1.DataKeyNames = new String[] { "isbn" };

            //计算购物车物品总价格
            double sumPrice = 0;
            for (int i = 0; i < shopCartList.Count; i++)
            {
                sumPrice += ((shopObject)shopCartList[i]).Bprice * ((shopObject)shopCartList[i]).Count;
            }
            sumCount.Text = "购物车总价格为:" + sumPrice + "元";
        }
    }