如何向购物车表中插入多条数据,如果购物车中存在该数据,就只更新数量,我用的是asp.net+sql server 2005.
2个回答
展开全部
//向购物车中添加商品,如果购物车中已经存在该商品,则商品数量加1,如果是第一次购买,则向购物车中添加一条商品信息
if(!IsPostBack)
{
string P_str_CartID = Session["member"].ToString(); 会员id
int P_str_GoodsID =Convert.ToInt32(Request["id"]); 产品id
DataSet ds = DB.CheckMember("select count(*) from ShoppingCar where CartID='"+ P_str_CartID+ "' and GoodsID=" + P_str_GoodsID);
if (ds.Tables[0].Rows[0][0].ToString() == "0")
{
DataSet ds1 = DB.CheckMember("select pro_name,pro_price from Product where id=" + P_str_GoodsID);
string P_str_GoodsName = ds1.Tables[0].Rows[0][0].ToString();
string P_str_GoodsPrice = ds1.Tables[0].Rows[0][1].ToString();
string P_str_Num = "1";
DB.ExSql("insert into ShoppingCar(CartID,GoodsID,GoodsName,GoodsPrice,GoodNum) values('" + P_str_CartID + "'," + P_str_GoodsID + ",'" + P_str_GoodsName + "'," + P_str_GoodsPrice + "," + P_str_Num + ")");
}
else
{
DB.ExSql("update ShoppingCar set GoodNum=GoodNum+1 where CartID='" + P_str_CartID + "' and GoodsID=" + P_str_GoodsID);
}
Bind();//显示购物车中的所有商品信息
}
这个是我写的 你可以参考下
if(!IsPostBack)
{
string P_str_CartID = Session["member"].ToString(); 会员id
int P_str_GoodsID =Convert.ToInt32(Request["id"]); 产品id
DataSet ds = DB.CheckMember("select count(*) from ShoppingCar where CartID='"+ P_str_CartID+ "' and GoodsID=" + P_str_GoodsID);
if (ds.Tables[0].Rows[0][0].ToString() == "0")
{
DataSet ds1 = DB.CheckMember("select pro_name,pro_price from Product where id=" + P_str_GoodsID);
string P_str_GoodsName = ds1.Tables[0].Rows[0][0].ToString();
string P_str_GoodsPrice = ds1.Tables[0].Rows[0][1].ToString();
string P_str_Num = "1";
DB.ExSql("insert into ShoppingCar(CartID,GoodsID,GoodsName,GoodsPrice,GoodNum) values('" + P_str_CartID + "'," + P_str_GoodsID + ",'" + P_str_GoodsName + "'," + P_str_GoodsPrice + "," + P_str_Num + ")");
}
else
{
DB.ExSql("update ShoppingCar set GoodNum=GoodNum+1 where CartID='" + P_str_CartID + "' and GoodsID=" + P_str_GoodsID);
}
Bind();//显示购物车中的所有商品信息
}
这个是我写的 你可以参考下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询