Exemplo n.º 1
0
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(BookShop.Model.Cart model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update Cart set ");
            strSql.Append("UserId=@UserId,");
            strSql.Append("BookId=@BookId,");
            strSql.Append("Count=@Count ");

            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@Id",     SqlDbType.Int, 4),
                new SqlParameter("@UserId", SqlDbType.Int, 4),
                new SqlParameter("@BookId", SqlDbType.Int, 4),
                new SqlParameter("@Count",  SqlDbType.Int, 4)
            };
            parameters[0].Value = model.Id;
            parameters[1].Value = model.User.Id;
            parameters[2].Value = model.Book.Id;
            parameters[3].Value = model.Count;


            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BookShop.Model.Cart model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update Cart set ");
            strSql.Append("UserId=@UserId,");
            strSql.Append("BookId=@BookId,");
            strSql.Append("Count=@Count");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@UserId", SqlDbType.Int, 4),
                new SqlParameter("@BookId", SqlDbType.Int, 4),
                new SqlParameter("@Count",  SqlDbType.Int, 4),
                new SqlParameter("@Id",     SqlDbType.Int, 4)
            };
            parameters[0].Value = model.UserId;
            parameters[1].Value = model.BookId;
            parameters[2].Value = model.Count;
            parameters[3].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            if (rows > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BookShop.Model.Cart model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into Cart(");
            strSql.Append("UserId,BookId,Count )");
            strSql.Append(" values (");
            strSql.Append("@UserId,@BookId,@Count)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                new SqlParameter("@UserId", SqlDbType.Int, 4),
                new SqlParameter("@BookId", SqlDbType.Int, 4),
                new SqlParameter("@Count",  SqlDbType.Int, 4)
            }

            ;
            parameters[0].Value = model.User.Id;
            parameters[1].Value = model.Book.Id;
            parameters[2].Value = model.Count;


            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);

            if (obj == null)
            {
                return(1);
            }
            else
            {
                return(Convert.ToInt32(obj));
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// 得到一个对象实体
 /// </summary>
 public BookShop.Model.Cart DataRowToModel(DataRow row)
 {
     BookShop.Model.Cart model = new BookShop.Model.Cart();
     if (row != null)
     {
         if (row["Id"] != null && row["Id"].ToString() != "")
         {
             model.Id = int.Parse(row["Id"].ToString());
         }
         if (row["UserId"] != null && row["UserId"].ToString() != "")
         {
             model.UserId = int.Parse(row["UserId"].ToString());
         }
         if (row["BookId"] != null && row["BookId"].ToString() != "")
         {
             int bookid = int.Parse(row["BookId"].ToString());
             model.Book = new BooksDAL().GetModel(bookid);
         }
         if (row["Count"] != null && row["Count"].ToString() != "")
         {
             model.Count = int.Parse(row["Count"].ToString());
         }
     }
     return model;
 }
Exemplo n.º 5
0
        //------------------------------
        /// <summary>
        /// 查询一下当前用户是否已经选择了该商品。
        /// </summary>
        /// <param name="bookId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Model.Cart GetCartModel(int bookId, int userId)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 Id,UserId,BookId,Count from Cart ");
            strSql.Append(" where BookId=@BookId and UserId=@UserId ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@BookId", SqlDbType.Int, 4),

                new SqlParameter("@UserId", SqlDbType.Int, 4)
            };
            parameters[0].Value = bookId;
            parameters[1].Value = userId;

            BookShop.Model.Cart model = new BookShop.Model.Cart();
            DataSet             ds    = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    int UserId = int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                    model.User = userServices.GetModel(UserId);
                }
                if (ds.Tables[0].Rows[0]["BookId"].ToString() != "")
                {
                    int BookId = int.Parse(ds.Tables[0].Rows[0]["BookId"].ToString());
                    model.Book = bookServices.GetModel(BookId);
                }
                if (ds.Tables[0].Rows[0]["Count"].ToString() != "")
                {
                    model.Count = int.Parse(ds.Tables[0].Rows[0]["Count"].ToString());
                }

                return(model);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BookShop.Model.Cart model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into Cart(");
            strSql.Append("UserId,BookId,Count )");
            strSql.Append(" values (");
            strSql.Append("@UserId,@BookId,@Count)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                new SqlParameter("@UserId", SqlDbType.Int, 4),
                new SqlParameter("@BookId", SqlDbType.Int, 4),
                new SqlParameter("@Count",  SqlDbType.Int, 4)
            }

            ;
            //3层时候 也早就可以从一个对象 点出另外一个对象的数据。
            //珍惜可以安静的 好好写代码的时候。有很多时候 看着能写代码
            //其实 不是的。需要一个不错的环境 环境的要求还挺高的
            // 一个表里面有多个 外键的时候 他们的关系 怎么样来理解
            //这里 实体类型的关系 是被改造过的。。 先一点一点的全部真的弄懂。
            //一个用户一个购物车
            parameters[0].Value = model.User.Id;
            parameters[1].Value = model.Book.Id;
            parameters[2].Value = model.Count;


            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);

            if (obj == null)
            {
                return(1);
            }
            else
            {
                return(Convert.ToInt32(obj));
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// 向购物车中添加商品信息。
 /// </summary>
 protected void AddCart()
 {
     if (!string.IsNullOrEmpty(Request.QueryString["id"]))
     {
         int bookId = 0;
         if (!int.TryParse(Request.QueryString["id"], out bookId))
         {
             Response.Redirect("/showinfo.aspx?msg=" + Server.UrlEncode("参数错误") + "&url=BookList.aspx" + "&txt=" + Server.UrlEncode("返回图书列表"));
         }
         else
         {
             BLL.BookManager bll   = new BookShop.BLL.BookManager();
             Model.Book      model = bll.GetModel(bookId);//根据传递过来的书的编号查找该书.
             if (model != null)
             {
                 BLL.CartManager cartBll   = new BookShop.BLL.CartManager();
                 int             userId    = ((Model.User)Session["user"]).Id;   //得到了当前登录用户的编号.
                 Model.Cart      cartModel = cartBll.GetModel(model.Id, userId); //根据用户的编号,与书的编号,找出购物车中的商品项.
                 if (cartModel == null)                                          //如果该条件成立,向购物车表中插入一条记录
                 {
                     Model.Cart ModelCart = new BookShop.Model.Cart();
                     ModelCart.User  = (Model.User)Session["user"];
                     ModelCart.Book  = model;
                     ModelCart.Count = 1;
                     cartBll.Add(ModelCart);
                 }
                 else//更新该商品项的数量
                 {
                     cartModel.Count = cartModel.Count + 1;
                     cartBll.Update(cartModel);
                 }
             }
             else
             {
                 Response.Redirect("/showinfo.aspx?msg=" + Server.UrlEncode("该书不存在") + "&url=BookList.aspx" + "&txt=" + Server.UrlEncode("返回图书列表"));
             }
         }
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// 得到一个对象实体
 /// </summary>
 public BookShop.Model.Cart DataRowToModel(DataRow row)
 {
     BookShop.Model.Cart model = new BookShop.Model.Cart();
     if (row != null)
     {
         if (row["Id"] != null && row["Id"].ToString() != "")
         {
             model.Id = int.Parse(row["Id"].ToString());
         }
         if (row["UserId"] != null && row["UserId"].ToString() != "")
         {
             model.UserId = int.Parse(row["UserId"].ToString());
         }
         if (row["BookId"] != null && row["BookId"].ToString() != "")
         {
             model.BookId = int.Parse(row["BookId"].ToString());
         }
         if (row["Count"] != null && row["Count"].ToString() != "")
         {
             model.Count = int.Parse(row["Count"].ToString());
         }
     }
     return(model);
 }
Exemplo n.º 9
0
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BookShop.Model.Cart GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 Id,UserId,BookId,Count from Cart ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@Id", SqlDbType.Int, 4)
            };
            parameters[0].Value = Id;

            BookShop.Model.Cart model = new BookShop.Model.Cart();
            DataSet             ds    = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                return(DataRowToModel(ds.Tables[0].Rows[0]));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BookShop.Model.Cart GetModel(int userId, int bookId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,UserId,BookId,Count from Cart ");
            strSql.Append(" where UserId=@UserId and BookId=@BookId");
            SqlParameter[] parameters = {
                    new SqlParameter("@UserId", SqlDbType.Int,4),
                    new SqlParameter("@BookId", SqlDbType.Int,4)
            };
            parameters[0].Value = userId;
            parameters[1].Value = bookId;

            BookShop.Model.Cart model = new BookShop.Model.Cart();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }