📄 catalogaccess.cs
字号:
using System;
using System.Data;
using System.Data.Common;
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;
public struct DepartmentDetails
{
public string Name;
public string Description;
}
public struct CategoryDetails
{
public int DepartmentId;
public string Name;
public string Description;
}
public struct ProductDetails
{
public string Name;
public string Description;
public decimal Price;
public string Image1FileName;
public string Image2FileName;
public bool OnDepartmentPromotion;
public bool OnCatalogPromotion;
}
/// <summary>
/// CatalogAccess 的摘要说明
/// </summary>
public class CatalogAccess
{
public CatalogAccess()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//执行存储过程
public static DataTable GetDepartments()
{
DbCommand comm = GenericDataAccess.CreatCommand();
comm.CommandText = "GetDepartments";
return GenericDataAccess.ExecuteSelectCommand(comm);
}
//得到门类列表
public static DepartmentDetails GetDepartmentDetails(string departmentId)
{
DbCommand comm = GenericDataAccess.CreatCommand();
comm.CommandText = "GetDepartmentDetails";
DbParameter param = comm.CreateParameter();
param.ParameterName = "@DepartmentID";
param.Value = departmentId;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
DepartmentDetails details = new DepartmentDetails();
if(table.Rows.Count>0)
{
details.Name = table.Rows[0]["Name"].ToString();
details.Description = table.Rows[0]["Description"].ToString();
}
return details;
}
// 得到分类列表
public static CategoryDetails GetCategoryDetails(string categoryId)
{
DbCommand comm = GenericDataAccess.CreatCommand();
comm.CommandText = "GetCategoryDetails";
DbParameter param = comm.CreateParameter();
param.ParameterName = "@CategoryID";
param.Value = categoryId;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
CategoryDetails details = new CategoryDetails();
if(table.Rows.Count>0)
{
details.DepartmentId = Int32.Parse(table.Rows[0]["DepartmentID"].ToString());
details.Name = table.Rows[0]["Name"].ToString();
details.Description = table.Rows[0]["Description"].ToString();
}
return details;
}
//得到产品列表
public static ProductDetails GetProductDetails(string productId)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "GetProductDetails";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@ProductID";
param.Value = productId;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// execute the stored procedure
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
// wrap retrieved data into a ProductDetails object
ProductDetails details = new ProductDetails();
if (table.Rows.Count > 0)
{
// get the first table row
DataRow dr = table.Rows[0];
// get product details
details.Name = dr["Name"].ToString();
details.Description = dr["Description"].ToString();
details.Price =Decimal.Parse(dr["Price"].ToString());
details.Image1FileName = dr["Image1FileName"].ToString();
details.Image2FileName = dr["Image2FileName"].ToString();
details.OnDepartmentPromotion = bool.Parse(dr["OnDepartmentPromotion"].ToString());
details.OnCatalogPromotion = bool.Parse(dr["OnCatalogPromotion"].ToString());
}
// return department details
return details;
}
// 得到某门类下的分类列表
public static DataTable GetCategoriesInDepartment(string departmentId)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "GetCategoriesInDepartment";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@DepartmentID";
param.Value = departmentId;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// execute the stored procedure
return GenericDataAccess.ExecuteSelectCommand(comm);
}
// 显示在网站主页面的特色商品
public static DataTable GetProductsOnCatalogPromotion(string pageNumber, out int howManyPages)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "GetProductsOnCatalogPromotion";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@DescriptionLength";
param.Value = BalloonShopConfiguration.ProductDescriptionLength;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@PageNumber";
param.Value = pageNumber;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@ProductsPerPage";
param.Value = BalloonShopConfiguration.ProductsPerPage;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@HowManyProducts";
param.Direction = ParameterDirection.Output;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// execute the stored procedure and save the results in a DataTable
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
// calculate how many pages of products and set the out parameter
int howManyProducts = Int32.Parse(comm.Parameters["@HowManyProducts"].Value.ToString());
howManyPages = (int)Math.Ceiling((double)howManyProducts /
(double)BalloonShopConfiguration.ProductsPerPage);
// return the page of products
return table;
}
// 在顾客访问门类主页时显示指定门类的特色商品列表
public static DataTable GetProductsOnDepartmentPromotion(string departmentId, string pageNumber, out int howManyPages)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "GetProductsOnDepartmentPromotion";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@DepartmentID";
param.Value = departmentId;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@DescriptionLength";
param.Value = BalloonShopConfiguration.ProductDescriptionLength;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@PageNumber";
param.Value = pageNumber;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@ProductsPerPage";
param.Value = BalloonShopConfiguration.ProductsPerPage;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@HowManyProducts";
param.Direction = ParameterDirection.Output;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// execute the stored procedure and save the results in a DataTable
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
// calculate how many pages of products and set the out parameter
int howManyProducts = Int32.Parse(comm.Parameters["@HowManyProducts"].Value.ToString());
howManyPages = (int)Math.Ceiling((double)howManyProducts /
(double)BalloonShopConfiguration.ProductsPerPage);
// return the page of products
return table;
}
// 返回分类下的商品列表
public static DataTable GetProductsInCategory(string categoryId, string pageNumber, out int howManyPages)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "GetProductsInCategory";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@CategoryID";
param.Value = categoryId;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@DescriptionLength";
param.Value = BalloonShopConfiguration.ProductDescriptionLength;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@PageNumber";
param.Value = pageNumber;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@ProductsPerPage";
param.Value = BalloonShopConfiguration.ProductsPerPage;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@HowManyProducts";
param.Direction = ParameterDirection.Output;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// execute the stored procedure and save the results in a DataTable
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
// calculate how many pages of products and set the out parameter
int howManyProducts = Int32.Parse(comm.Parameters["@HowManyProducts"].Value.ToString());
howManyPages = (int)Math.Ceiling((double)howManyProducts /
(double)BalloonShopConfiguration.ProductsPerPage);
// return the page of products
return table;
}
//为搜寻UserControl添加Search方法
public static DataTable Search(string searchString, string allWords, string pageNumber, out int howManyPages)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreatCommand();
// set the stored procedure name
comm.CommandText = "SearchCatalog";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@DescriptionLength";
param.Value = BalloonShopConfiguration.ProductDescriptionLength;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@AllWords";
param.Value = allWords.ToUpper() == "TRUE" ? "True" : "False";
param.DbType = DbType.Boolean;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@PageNumber";
param.Value = pageNumber;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@ProductsPerPage";
param.Value = BalloonShopConfiguration.ProductsPerPage;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// create a new parameter
param = comm.CreateParameter();
param.ParameterName = "@HowManyResults";
param.Direction = ParameterDirection.Output;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// define the maximum number of words
int howManyWords = 5;
// transform search string into array of words
char[] wordSeparators = new char[] { ',', ';', '.', '!', '?', '-', ' ' };
string[] words = searchString.Split(wordSeparators, StringSplitOptions.RemoveEmptyEntries);
int index = 1;
// add the words as stored procedure parameters
for (int i = 0; i <= words.GetUpperBound(0) && index <= howManyWords; i++)
// ignore short words
if (words[i].Length > 2)
{
// create the @Word parameters
param = comm.CreateParameter();
param.ParameterName = "@Word" + index.ToString();
param.Value = words[i];
param.DbType = DbType.String;
comm.Parameters.Add(param);
index++;
}
// execute the stored procedure and save the results in a DataTable
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
// calculate how many pages of products and set the out parameter
int howManyProducts = Int32.Parse(comm.Parameters["@HowManyResults"].Value.ToString());
howManyPages = (int)Math.Ceiling((double)howManyProducts /
(double)BalloonShopConfiguration.ProductsPerPage);
// return the page of products
return table;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -