---2 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN 1 Xây dưng thành phần Data Tier 1.1 Tạo các Data Table 1.1.1 Category USE BalloonShop GO CREATE TABLE Category CategoryID INT
Trang 11 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN
1
Môn học:
Phát triển ứng dụng Web nâng cao với ASP.NET
Product Catalog – P2
TR ƯỜ NG ĐẠ I H Ọ C KHOA H Ọ C T Ự NHIÊN
KHOA CÔNG NGH Ệ THÔNG TIN
B Ộ MÔN CÔNG NGH Ệ PH Ầ N M Ề M
-
Trang 2-2 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN
1 Xây dưng thành phần Data Tier
1.1 Tạo các Data Table
1.1.1 Category
USE BalloonShop
GO
CREATE TABLE Category(
CategoryID INT IDENTITY( , ) NOT NULL,
DepartmentID INT NOT NULL,
Name NVARCHAR(50) NOT NULL,
Description NVARCHAR(1000) NULL,
CONSTRAINT PK_Category_1 PRIMARY KEY CLUSTERED(CategoryID ASC)
INSERT INTO Category (CategoryID, DepartmentID, Name, Description
VALUES 1 1, 'Love & Romance', 'Here''s our collection of balloons with
romantic messages.')
INSERT INTO Category (CategoryID, DepartmentID, Name, Description
VALUES 2 1, 'Birthdays', 'Tell someone "Happy Birthday" with one of these wonderful balloons!')
INSERT INTO Category (CategoryID, DepartmentID, Name, Description
VALUES 3 1, 'Weddings', 'Going to a wedding? Here''s a collection of
balloons for that special event!')
INSERT INTO Category (CategoryID, DepartmentID, Name, Description
VALUES 4 2, 'Message Balloons', 'Why write on paper, when you can deliver your message on a balloon?')
Trang 33 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN
INSERT INTO Category (CategoryID, DepartmentID, Name, Description
VALUES 5 2, 'Cartoons', 'Buy a balloon with your child''s favorite cartoon character!')
INSERT INTO Category (CategoryID, DepartmentID, Name, Description
VALUES 6 2, 'Miscellaneous', 'Various baloons that your kid will most
CREATE TABLE Product(
ProductID INT IDENTITY( , ) NOT NULL,
Name NVARCHAR(50) NOT NULL,
Description NVARCHAR(MAX) NOT NULL,
Price MONEY NOT NULL,
Image1FileName NVARCHAR(50) NULL,
Image2FileName NVARCHAR(50) NULL,
OnCatalogPromotion BIT NOT NULL,
OnDepartmentPromotion BIT NOT NULL,
CONSTRAINT PK_Product PRIMARY KEY CLUSTERED ProductID ASC)
INSERT INTO Product(ProductID, Name, Description, Price, Image1FileName,
Image2FileName, OnCatalogPromotion, OnDepartmentPromotion )
VALUES 1 'I Love You (Simon Elvin)', 'An adorable romantic balloon by Simon Elvin You''ll fall in love with the cute bear bearing a bouquet of roses, a heart with I Love You, and a card.', 121.9900, 't0326801.jpg', '0326801.jpg',
0 1)
GO
Trang 44 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN
Ghi chú: Sử dụng các file script được cung cấp sẵn để tạo đầy đủ CSDL
1.1.3 ProductCategory
USE BalloonShop
GO
CREATE TABLE ProductCategory(
ProductID INT NOT NULL,
CategoryID INT NOT NULL,
CONSTRAINT PK_ProductCategory PRIMARY KEY CLUSTERED ProductID ASC,
CategoryID ASC)
)
GO
ALTER TABLE ProductCategory WITH CHECK ADD CONSTRAINT
FK_ProductCategory_Category FOREIGN KEY(CategoryID)
REFERENCES Category (CategoryID)
GO
ALTER TABLE ProductCategory WITH CHECK ADD CONSTRAINT
FK_ProductCategory_Product FOREIGN KEY(ProductID)
REFERENCES Product (ProductID)
Trang 55 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN
declare a new TABLE variable
DECLARE @Products TABLE
populate the table variable with the complete list of products
INSERT INTO @Products
SELECT ROW_NUMBER() OVER ORDER BY Product.ProductID),
ProductID, Name,
Trang 66 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN
CASE WHEN LEN(Description) <= @DescriptionLength THEN Description ELSE SUBSTRING(Description, 1, @DescriptionLength) + ' ' END
AS Description, Price, Image1FileName, Image2FileName,
OnCatalogPromotion, OnDepartmentPromotion
FROM Product
WHERE OnCatalogPromotion = 1
return the total number of products using an OUTPUT variable
SELECT @HowManyProducts = COUNT(ProductID) FROM @Products
extract the requested page of products
SELECT ProductID, Name, Description, Price, Image1FileName,
Image2FileName, OnCatalogPromotion, OnDepartmentPromotion
FROM @Products
WHERE RowNumber > (@PageNumber - 1) * @ProductsPerPage
AND RowNumber <= @PageNumber * @ProductsPerPage
declare a new TABLE variable
DECLARE @Products TABLE
populate the table variable with the complete list of products
INSERT INTO @Products
SELECT ROW_NUMBER() OVER ORDER BY Product.ProductID),
Product.ProductID, Name,
CASE WHEN LEN(Description) <= @DescriptionLength THEN Description ELSE SUBSTRING(Description, 1, @DescriptionLength) + ' ' END
AS Description, Price, Image1FileName, Image2FileName,
OnCatalogPromotion, OnDepartmentPromotion
FROM Product INNER JOIN ProductCategory
ON Product.ProductID = ProductCategory.ProductID
WHERE ProductCategory.CategoryID = @CategoryID
return the total number of products using an OUTPUT variable
SELECT @HowManyProducts = COUNT(ProductID) FROM @Products
extract the requested page of products
Trang 77 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN
SELECT ProductID, Name, Description, Price, Image1FileName,
Image2FileName, OnCatalogPromotion, OnDepartmentPromotion
FROM @Products
WHERE RowNumber > (@PageNumber - 1) * @ProductsPerPage
AND RowNumber <= @PageNumber * @ProductsPerPage
declare a new TABLE variable
DECLARE @Products TABLE
populate the table variable with the complete list of products
INSERT INTO @Products
SELECT ROW_NUMBER() OVER ORDER BY ProductID) AS Row,
ProductID, Name, SUBSTRING(Description, 1, @DescriptionLength)
+ ' ' AS Description,
Price, Image1FileName, Image2FileName, OnCatalogPromotion,
OnDepartmentPromotion
FROM
(SELECT DISTINCT Product.ProductID, Product.Name,
CASE WHEN LEN(Product.Description) <= @DescriptionLength
THEN Product.Description
ELSE SUBSTRING(Product.Description, 1, @DescriptionLength) +
' ' END
AS Description, Price, Image1FileName, Image2FileName,
OnCatalogPromotion, OnDepartmentPromotion
FROM Product INNER JOIN ProductCategory
ON Product.ProductID = ProductCategory.ProductID
INNER JOIN Category
ON ProductCategory.CategoryID = Category.CategoryID WHERE Product.OnDepartmentPromotion = 1
AND Category.DepartmentID = @DepartmentID
) AS ProductOnDepPr
return the total number of products using an OUTPUT variable
SELECT @HowManyProducts = COUNT(ProductID) FROM @Products
Trang 88 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN
extract the requested page of products
SELECT ProductID, Name, Description, Price, Image1FileName,
Image2FileName, OnCatalogPromotion, OnDepartmentPromotion
FROM @Products
WHERE RowNumber > (@PageNumber - 1) * @ProductsPerPage
AND RowNumber <= @PageNumber * @ProductsPerPage
<add key= ProductsPerPage" value= 6 />
<add key= ProductDescriptionLength" value= 60"/>
<add key= SiteName" value= BalloonShop"/>
</appSettings>
2.2 BalloonShopConfiguration class
- Thêm và thay đổi một số thành phần
private readonly static int productsPerPage;
public static int ProductsPerPage
{
get { return BalloonShopConfiguration.productsPerPage; }
}
private readonly static int productDescriptionLength;
public static int ProductDescriptionLength
{
get { return BalloonShopConfiguration.productDescriptionLength; } }
private readonly static string siteName;
public static string SiteName
{
get { return BalloonShopConfiguration.siteName; }
}
Trang 99 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN
public string Name;
public string Description;
Trang 1010 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN
- Thêm method (hàm) GetDepartmentDetails
public static DepartmentDetails GetDepartmentDetails(string departmentId) {
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
// set the stored procedure name
comm.CommandText = "GetDepartmentDetails";
// 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
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
// wrap retrieved data into a DepartmentDetails object
DepartmentDetails details = new DepartmentDetails();
if (table.Rows.Count > 0)
{
details.Name = table.Rows[0]["Name"].ToString();
details.Description = table.Rows[0]["Description"].ToString(); }
// return department details
return details;
}
2.3.2 CategoryDetails
- Thêm thành phần struct CategoryDetails
public struct CategoryDetails
{
public int DepartmentId;
public string Name;
public string Description;
}
- Thêm method (hàm) GetCategoryDetails
public static CategoryDetails GetCategoryDetails(string categoryId)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
// set the stored procedure name
comm.CommandText = "GetCategoryDetails";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@CategoryID";
param.Value = categoryId;
param.DbType = DbType.Int32;
Trang 1111 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN
comm.Parameters.Add(param);
// execute the stored procedure
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
// wrap retrieved data into a CategoryDetails object
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 department details
return details;
}
2.3.3 ProductDetails
- Thêm thành phần struct ProductDetails
public struct ProductDetails
{
public string Name;
public string Description;
public decimal Price;
public string Image1FileName;
public string Image2FileName;
public bool OnDepartmentPromotion;
public bool OnCatalogPromotion;
}
- Thêm method (hàm) GetProductDetails
public static ProductDetails GetProductDetails(string productId)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
// 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
Trang 1212 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN
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();
- Thêm method (hàm) GetCategoryinDepartment
public static DataTable GetCategoriesInDepartment(string departmentId)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
// 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);
}
- Thêm method (hàm) GetProductsOnCatalogPromotion
public static DataTable GetProductsOnCatalogPromotion(string pageNumber, out int howManyPages)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
// set the stored procedure name
comm.CommandText = "GetProductsOnCatalogPromotion";
// create a new parameter
DbParameter param = comm.CreateParameter();
Trang 1313 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN
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;
}
- Thêm method (hàm) GetProductsOnCatalogPromotion
public static DataTable GetProductsOnDepartmentPromotion
(string departmentId, string pageNumber, out int howManyPages)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
// set the stored procedure name
comm.CommandText = "GetProductsOnDepartmentPromotion";
// create a new parameter
DbParameter param = comm.CreateParameter();
Trang 1414 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN
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;
}
- Thêm method (hàm) GetProductsInCategory
public static DataTable GetProductsInCategory
(string categoryId, string pageNumber, out int howManyPages)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
// set the stored procedure name
comm.CommandText = "GetProductsInCategory";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.Value = BalloonShopConfiguration.ProductsPerPage;
param.DbType = DbType.Int32;