📄 shoppingcartaccess.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
/// <summary>
/// ShoppingCartAccess 的摘要说明
/// 对购物车的业务逻辑操作
/// </summary>
public class ShoppingCartAccess
{
public ShoppingCartAccess()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region
// 这个方法生成购物车的自动唯一标识ID号
private static string shoppingCartId
{
get
{
// get the current HttpContext
HttpContext context = HttpContext.Current;
// try to retrieve the cart ID from the user session object
string cartId = "";
object cartIdSession = context.Session["BalloonShop_CartID"];
if (cartIdSession != null)
cartId = cartIdSession.ToString();
// if the ID exists in the current session...
if (cartId != "")
// return its value
return cartId;
else
// if the cart ID isn't in the session...
{
// check if the cart ID exists as a cookie
if (context.Request.Cookies["BalloonShop_CartID"] != null)
{
// if the cart exists as a cookie, use the cookie to get its value
cartId = context.Request.Cookies["BalloonShop_CartID"].Value;
// save the id to the session, to avoid reading the cookie next time
context.Session["BalloonShop_CartID"] = cartId;
// return the id
return cartId;
}
else
// if the cart ID doesn't exist in the cookie as well, generate a new ID
{
// generate a new GUID
cartId = Guid.NewGuid().ToString();
// create the cookie object and set its value
HttpCookie cookie = new HttpCookie("BalloonShop_CartID", cartId.ToString());
// set the cookie's expiration date
int howManyDays = BalloonShopConfiguration.CartPersistDays;
DateTime currentDate = DateTime.Now;
TimeSpan timeSpan = new TimeSpan(howManyDays, 0, 0, 0);
DateTime expirationDate = currentDate.Add(timeSpan);
cookie.Expires = expirationDate;
// set the cookie on client's browser
context.Response.Cookies.Add(cookie);
// save the ID to the Session as well
context.Session["BalloonShop_CartID"] = cartId;
// return the CartID
return cartId.ToString();
}
}
}
}
#endregion
#region
//添加新商品到购物车
public static bool AddItem(string productId)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "ShoppingCartAddItem";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@CartID";
param.Value = shoppingCartId;
param.DbType = DbType.String;
param.Size = 36;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@ProductID";
param.Value = productId;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// returns true in case of success or false in case of an error
try
{
// execute the stored procedure and return true if it executes
// successfully, or false otherwise
return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
}
catch
{
// prevent the exception from propagating, but return false to
// signal the error
return false;
}
}
#endregion
#region
//更新购物车
public static bool UpdateItem(string productId, int quantity)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "ShoppingCartUpdateItem";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@CartID";
param.Value = shoppingCartId;
param.DbType = DbType.String;
param.Size = 36;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@ProductID";
param.Value = productId;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@Quantity";
param.Value = quantity;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// returns true in case of success or false in case of an error
try
{
// execute the stored procedure and return true if it executes
// successfully, or false otherwise
return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
}
catch
{
// prevent the exception from propagating, but return false to
// signal the error
return false;
}
}
#endregion
#region
//移除购物车
public static bool RemoveItem(string productId)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "ShoppingCartRemoveItem";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@CartID";
param.Value = shoppingCartId;
param.DbType = DbType.String;
param.Size = 36;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@ProductID";
param.Value = productId;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// returns true in case of success or false in case of an error
try
{
// execute the stored procedure and return true if it executes
// successfully, or false otherwise
return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
}
catch
{
// prevent the exception from propagating, but return false to
// signal the error
return false;
}
}
#endregion
#region
//获取顾客购物车中的所有商品
public static DataTable GetItems()
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "ShoppingCartGetItems";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@CartID";
param.Value = shoppingCartId;
param.DbType = DbType.String;
param.Size = 36;
comm.Parameters.Add(param);
// return the result table
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
return table;
}
#endregion
#region
//得到购物车的总价 返回一个数值
public static decimal GetTotalAmount()
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "ShoppingCartGetTotalAmount";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@CartID";
param.Value = shoppingCartId;
param.DbType = DbType.String;
param.Size = 36;
comm.Parameters.Add(param);
// return the result table
return Decimal.Parse(GenericDataAccess.ExecuteScalar(comm));
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -