?? cart.cs
字號:
?using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 購物籃的相關操作方法
/// </summary>
public class Cart
{
private const string PARM_PRODUCT_ID = "@ProductId";
private const string SQL_SELECT_ITEMS_BY_PRODUCT = "SELECT Item.ItemId, Item.ProductName, ProductCount.Count, Item.Price, Item.ProductImage, Item.SupplierId, Product.ProductId FROM Item INNER JOIN Product ON Item.ProductId = Product.ProductId INNER JOIN ProductCount ON Item.ItemId = ProductCount.ItemId WHERE Item.ProductId = @ProductId";
private const string PARM_ITEM_ID = "@ItemId";
private const string SQL_SELECT_ITEM = "SELECT Item.ItemId, Item.ProductName, Item.Price, Item.ProductImage, Item.SupplierId, Product.ProductId FROM Item INNER JOIN Product ON Item.ProductId = Product.ProductId WHERE Item.ItemId = @ItemId";
public Cart() { }
/// <summary>
/// 根據(jù)產(chǎn)品ID獲取產(chǎn)品詳細信息
/// </summary>
/// <param name="productId">產(chǎn)品ID</param>
/// <returns>返回產(chǎn)品列表</returns>
public IList<ItemInfo> GetItemsByProduct(string productId)
{
IList<ItemInfo> itemsByProduct = new List<ItemInfo>();
SqlParameter parm = new SqlParameter(PARM_PRODUCT_ID, SqlDbType.Int);
parm.Value = productId;
//執(zhí)行查詢語句
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEMS_BY_PRODUCT, parm))
{
// 如果存在結果
while (rdr.Read())
{
ItemInfo item = new ItemInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetInt32(2), rdr.GetDecimal(3), rdr.GetString(4), rdr.GetInt32(5), rdr.GetInt32(6));
//將結果添加到數(shù)組
itemsByProduct.Add(item);
}
}
return itemsByProduct;
}
/// <summary>
/// 通過ID獲取商品詳細信息
/// </summary>
/// <param name="itemId">詳細資料ID</param>
/// <returns>一個圖書詳細信息實體</returns>
public ItemInfo GetItem(int itemId)
{
// 驗證輸入的ID
if (string.IsNullOrEmpty(itemId.ToString()))
return null;
//初始化一個詳細資料實體
ItemInfo item = null;
//創(chuàng)建參數(shù)
SqlParameter parm = new SqlParameter(PARM_ITEM_ID, SqlDbType.Int);
//為參數(shù)賦值
parm.Value = itemId;
//執(zhí)行查詢
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEM, parm)) {
if (rdr.Read())
item = new ItemInfo(rdr.GetInt32(0), rdr.GetString(1),0, rdr.GetDecimal(2), rdr.GetString(3), rdr.GetInt32(4), rdr.GetInt32(5));
else
item = new ItemInfo();
}
return item;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -