/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.cart model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tb_cart("); strSql.Append("ProductName,Counts,UserID"); strSql.Append(") values ("); strSql.Append("@ProductName,@Counts,@UserID"); strSql.Append(") "); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@ProductName", SqlDbType.NVarChar, 50), new SqlParameter("@Counts", SqlDbType.NChar, 10), new SqlParameter("@UserID", SqlDbType.Int, 4) }; parameters[0].Value = model.ProductName; parameters[1].Value = model.Counts; parameters[2].Value = model.UserID; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 移除购物车 /// </summary> /// <param name="Key">主键 0为清理所有的购物车信息</param> public static void Clear(string Key) { Model.users model = new BasePage().GetUserInfo(); BLL.cart cartbll = new BLL.cart(); Model.cart modelcart = new Model.cart(); if (Key == "0")//为0的时候清理全部购物车cookies { Utils.WriteCookie(DTKeys.COOKIE_SHOPPING_CART, "", -43200); #region 清空登录会员的购物车 if (model!=null) { cartbll.Delete_user(model.id); } #endregion } else { IDictionary<string, int> dic = GetCart(); if (dic != null) { dic.Remove(Key); #region 删除登录会员指定的购物车 if (model != null) { cartbll.Delete(model.id,Key); } #endregion AddCookies(JsonMapper.ToJson(dic)); } } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.cart model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tb_cart set "); strSql.Append(" ProductName = @ProductName , "); strSql.Append(" Counts = @Counts , "); strSql.Append(" UserID = @UserID "); strSql.Append(" where ID=@ID "); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@ProductName", SqlDbType.NVarChar, 50), new SqlParameter("@Counts", SqlDbType.NChar, 10), new SqlParameter("@UserID", SqlDbType.Int, 4) }; parameters[0].Value = model.ID; parameters[1].Value = model.ProductName; parameters[2].Value = model.Counts; parameters[3].Value = model.UserID; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 移除购物车 /// </summary> /// <param name="Key">主键 0为清理所有的购物车信息</param> public static void Clear(string Key) { Model.users model = new BasePage().GetUserInfo(); BLL.cart cartbll = new BLL.cart(); Model.cart modelcart = new Model.cart(); if (Key == "0")//为0的时候清理全部购物车cookies { Utils.WriteCookie(DTKeys.COOKIE_SHOPPING_CART, "", -43200); #region 清空登录会员的购物车 if (model != null) { cartbll.Delete_user(model.id); } #endregion } else { IDictionary <string, int> dic = GetCart(); if (dic != null) { dic.Remove(Key); #region 除登录会员指定的购物车 if (model != null) { cartbll.Delete(model.id, Key); } #endregion AddCookies(JsonMapper.ToJson(dic)); } } }
/// <summary> /// 添加到购物车 /// </summary> public static bool Add(string Key, int Quantity) { Model.users model = new BasePage().GetUserInfo(); BLL.cart cartbll = new BLL.cart(); Model.cart modelcart = new Model.cart(); IDictionary<string, int> dic = GetCart(); if (dic != null) { if (dic.ContainsKey(Key)) { dic[Key] += Quantity; #region 添加到数据库 if (model!=null) { DataTable dt = cartbll.GetList("ProductName='" + Key + "' and UserID='" + model.id + "'").Tables[0]; if (dt.Rows.Count == 1) { modelcart = cartbll.GetModel(int.Parse(dt.Rows[0][0].ToString())); if (modelcart != null) { modelcart.Counts = (int.Parse(modelcart.Counts)+1).ToString(); cartbll.Update(modelcart); } } } #endregion AddCookies(JsonMapper.ToJson(dic)); return true; } } else { dic = new Dictionary<string, int>(); } //不存在的则新增 dic.Add(Key, Quantity); #region 添加到数据库 if (model != null) { modelcart.ProductName = Key; modelcart.Counts = Quantity.ToString(); modelcart.UserID = model.id; cartbll.Add(modelcart); } #endregion AddCookies(JsonMapper.ToJson(dic)); return true; }
/// <summary> /// 添加到购物车 /// </summary> public static bool Add(string Key, int Quantity) { Model.users model = new BasePage().GetUserInfo(); BLL.cart cartbll = new BLL.cart(); Model.cart modelcart = new Model.cart(); IDictionary <string, int> dic = GetCart(); if (dic != null) { if (dic.ContainsKey(Key)) { dic[Key] += Quantity; #region 添加到数据库 if (model != null) { DataTable dt = cartbll.GetList("ProductName='" + Key + "' and UserID='" + model.id + "'").Tables[0]; if (dt.Rows.Count == 1) { modelcart = cartbll.GetModel(int.Parse(dt.Rows[0][0].ToString())); if (modelcart != null) { modelcart.Counts = (int.Parse(modelcart.Counts) + 1).ToString(); cartbll.Update(modelcart); } } } #endregion AddCookies(JsonMapper.ToJson(dic)); return(true); } } else { dic = new Dictionary <string, int>(); } //不存在的则新增 dic.Add(Key, Quantity); #region 添加到数据库 if (model != null) { modelcart.ProductName = Key; modelcart.Counts = Quantity.ToString(); modelcart.UserID = model.id; cartbll.Add(modelcart); } #endregion AddCookies(JsonMapper.ToJson(dic)); return(true); }
/// <summary> /// 得到一个对象实体 /// </summary> public Model.cart GetModel(int ID) { StringBuilder strSql = new StringBuilder(); strSql.Append("select ID, ProductName, Counts, UserID "); strSql.Append(" from tb_cart "); strSql.Append(" where ID=@ID"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4) }; parameters[0].Value = ID; Model.cart model = new 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()); } model.ProductName = ds.Tables[0].Rows[0]["ProductName"].ToString(); model.Counts = ds.Tables[0].Rows[0]["Counts"].ToString(); if (ds.Tables[0].Rows[0]["UserID"].ToString() != "") { model.UserID = int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString()); } return(model); } else { return(null); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Model.cart GetModel(int ID) { StringBuilder strSql = new StringBuilder(); strSql.Append("select ID, ProductName, Counts, UserID "); strSql.Append(" from tb_cart "); strSql.Append(" where ID=@ID"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int,4) }; parameters[0].Value = ID; Model.cart model = new 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()); } model.ProductName = ds.Tables[0].Rows[0]["ProductName"].ToString(); model.Counts = ds.Tables[0].Rows[0]["Counts"].ToString(); if (ds.Tables[0].Rows[0]["UserID"].ToString() != "") { model.UserID = int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString()); } return model; } else { return null; } }
private void change_car_count(HttpContext context) { string car_id = DTRequest.GetQueryString("car_id"); int quantity = DTRequest.GetQueryInt("quantity", 1); Model.users model = new BasePage().GetUserInfo(); if (model == null) { context.Response.Write("{\"status\":\"0\", \"msg\":\"对不起,请重新登录!\"}"); return; } BLL.cart cartbll = new BLL.cart(); Model.cart modelcart = new Model.cart(); DataTable dt = cartbll.GetList("ProductName='" + car_id + "' and UserID='" + model.id + "'").Tables[0]; if (dt.Rows.Count == 1) { modelcart = cartbll.GetModel(int.Parse(dt.Rows[0][0].ToString())); if (modelcart != null) { modelcart.Counts = quantity.ToString(); cartbll.Update(modelcart); } } if (ShopCart.Update(car_id, quantity)) { context.Response.Write("{\"status\":\"1\",\"msg\":\"修改购物车数量成功!\"}"); return; } else { context.Response.Write("{\"status\":\"0\",\"msg\":\"修改购物车数量失败!\"}"); return; } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.cart model) { return(dal.Update(model)); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.cart model) { return(dal.Add(model)); }