Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 22 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
22
Dung lượng
173,45 KB
Nội dung
1 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN 11 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 Phát tri ể n ứ ng d ụ ng Web ASP.NET – 08CDITEC 2010 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(1,1) NOT NULL, DepartmentID INT NOT NULL, Name NVARCHAR(50) NOT NULL, Description NVARCHAR(1000) NULL, CONSTRAINT PK_Category_1 PRIMARY KEY CLUSTERED(CategoryID ASC) ) GO ALTER TABLE Category ADD CONSTRAINT FK_Category_Department FOREIGN KEY(DepartmentID) REFERENCES Department (DepartmentID) GO USE BalloonShop GO TRUNCATE TABLE Category GO SET IDENTITY_INSERT Category ON GO 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?') Phát tri ể n ứ ng d ụ ng Web ASP.NET – 08CDITEC 2010 3 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 certainly love!') GO SET IDENTITY_INSERT Category OFF GO 1.1.2 Product USE BalloonShop GO CREATE TABLE Product( ProductID INT IDENTITY(1,1) 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) ) USE BalloonShop GO TRUNCATE TABLE Product GO SET IDENTITY_INSERT Product ON GO 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 Phát tri ể n ứ ng d ụ ng Web ASP.NET – 08CDITEC 2010 4 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) USE BalloonShop GO INSERT INTO ProductCategory(ProductID, CategoryID) VALUES(1, 1) GO 1.2 Thành phần Store Procedure - Ý nghĩa ? - Input và Output parameter USE BalloonShop GO CREATE PROCEDURE GetDepartmentDetails (@DepartmentID INT) AS SELECT Name, Description FROM Department WHERE DepartmentID = @DepartmentID Phát tri ể n ứ ng d ụ ng Web ASP.NET – 08CDITEC 2010 5 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN GO CREATE PROCEDURE GetCategoryDetails (@CategoryID INT) AS SELECT DepartmentID, Name, Description FROM Category WHERE CategoryID = @CategoryID GO CREATE PROCEDURE GetProductDetails (@ProductID INT) AS SELECT Name, Description, Price, Image1FileName, Image2FileName, OnCatalogPromotion, OnDepartmentPromotion FROM Product WHERE ProductID = @ProductID GO CREATE PROCEDURE GetCategoriesInDepartment (@DepartmentID INT) AS SELECT CategoryID, Name, Description FROM Category WHERE DepartmentID = @DepartmentID GO CREATE PROCEDURE GetProductsOnCatalogPromotion (@DescriptionLength INT, @PageNumber INT, @ProductsPerPage INT, @HowManyProducts INT OUTPUT) AS declare a new TABLE variable DECLARE @Products TABLE (RowNumber INT, ProductID INT, Name NVARCHAR(50), Description NVARCHAR(MAX), Price MONEY, Image1FileName NVARCHAR(50), Image2FileName NVARCHAR(50), OnCatalogPromotion bit, OnDepartmentPromotion bit) populate the table variable with the complete list of products INSERT INTO @Products SELECT ROW_NUMBER() OVER (ORDER BY Product.ProductID), ProductID, Name, Phát tri ể n ứ ng d ụ ng Web ASP.NET – 08CDITEC 2010 6 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 GO CREATE PROCEDURE GetProductsInCategory (@CategoryID INT, @DescriptionLength INT, @PageNumber INT, @ProductsPerPage INT, @HowManyProducts INT OUTPUT) AS declare a new TABLE variable DECLARE @Products TABLE (RowNumber INT, ProductID INT, Name NVARCHAR(50), Description NVARCHAR(MAX), Price MONEY, Image1FileName NVARCHAR(50), Image2FileName NVARCHAR(50), OnCatalogPromotion bit, OnDepartmentPromotion bit) 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 Phát tri ể n ứ ng d ụ ng Web ASP.NET – 08CDITEC 2010 7 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 GO CREATE PROCEDURE GetProductsOnDepartmentPromotion (@DepartmentID INT, @DescriptionLength INT, @PageNumber INT, @ProductsPerPage INT, @HowManyProducts INT OUTPUT) AS declare a new TABLE variable DECLARE @Products TABLE (RowNumber INT, ProductID INT, Name NVARCHAR(50), Description NVARCHAR(MAX), Price MONEY, Image1FileName NVARCHAR(50), Image2FileName NVARCHAR(50), OnCatalogPromotion bit, OnDepartmentPromotion bit) 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 Phát tri ể n ứ ng d ụ ng Web ASP.NET – 08CDITEC 2010 8 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 GO 2 Xây dựng thành phần Business Tier 2.1 Web.config - Thêm vào các thành phần trong web.config <appSettings> …………… <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; } } Phát tri ể n ứ ng d ụ ng Web ASP.NET – 08CDITEC 2010 9 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN static BalloonShopConfiguration() { dbConnectionString = ConfigurationManager.ConnectionStrings["BalloonShopConnection"].ConnectionStr ing; dbProviderName = ConfigurationManager.ConnectionStrings["BalloonShopConnection"].ProviderName; productsPerPage = Int32.Parse(ConfigurationManager.AppSettings["ProductsPerPage"].ToString()); productDescriptionLength = Int32.Parse(ConfigurationManager.AppSettings["ProductDescriptionLength"].ToSt ring()); } 2.3 CatalogAccess class - Thêm và thay đổi một số thành phần 2.3.1 DepartmentDetails - Thêm thành phần struct DepartmentDetails using System; using System.Data; using System.Data.Common; /// <summary> /// Summary description for CatalogAccess /// </summary> /// public struct DepartmentDetails { public string Name; public string Description; } public static class CatalogAccess { static CatalogAccess() { } } Phát tri ể n ứ ng d ụ ng Web ASP.NET – 08CDITEC 2010 10 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; [...]...Phát triển ứng dụng Web ASP.NET – 08CDITEC 2010 comm.Parameters.Add(param); // execute the stored procedure DataTable table = GenericDataAccess.ExecuteSelectCommand(comm); // wrap retrieved data into a CategoryDetails object... ProductDetails object ProductDetails details = new ProductDetails(); if (table.Rows.Count > 0) { // get the first table row 11 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN Phát triển ứng dụng Web ASP.NET – 08CDITEC 2010 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());... // create a new parameter param = comm.CreateParameter(); param.ParameterName = "@PageNumber"; param.Value = pageNumber; 12 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN Phát triển ứng dụng Web ASP.NET – 08CDITEC 2010 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(); 13 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN Phát triển ứng dụng Web ASP.NET – 08CDITEC 2010 param.ParameterName = "@ProductsPerPage"; param.Value = BalloonShopConfiguration.ProductsPerPage; param.DbType = DbType.Int32; comm.Parameters.Add(param); // create a new parameter... param.ParameterName = "@ProductsPerPage"; param.Value = BalloonShopConfiguration.ProductsPerPage; param.DbType = DbType.Int32; 14 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN Phát triển ứng dụng Web ASP.NET – 08CDITEC 2010 comm.Parameters.Add(param); // create a new parameter param = comm.CreateParameter(); param.ParameterName = "@HowManyProducts"; param.Direction = ParameterDirection.Output; param.DbType... Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string departmentId = Request.QueryString["DepartmentID"]; 15 Nguyễn Phạm Phương Nam - Bộ môn CNPM – Khoa CNTT - DHKHTN Phát triển ứng dụng Web ASP.NET – 08CDITEC 2010 if (departmentId != null) { DataTable dt = CatalogAccess.GetCategoriesInDepartment(departmentId); string categoryID = Request.QueryString["CategoryID"]; if (!string.IsNullOrEmpty(categoryID))... Request.QueryString["DepartmentID"]) { e.Item.FindControl("CategoriesList1").Visible = false; } } - Tạo trang Catalog.aspx và test project 16 Trần Phượng Hoàng – PTN BMCNPM – Khoa CNTT – DHKHTN || != Phát triển ứng dụng Web ASP.NET – 08CDITEC 2010 3.2 List of Products - Tạo User Control ProductsList.ascx - Thành phần GUI - Kéo thả ProductList.ascx . 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 Phát tri ể n ứ ng d ụ ng Web ASP. NET – 08CDITEC . e.Item.FindControl("CategoriesList1").Visible = false; } } - Tạo trang Catalog.aspx và test project Phát tri ể n ứ ng d ụ ng Web ASP. NET – 08CDITEC 2010 17 Nguyễn Phạm Phương Nam - Bộ môn CNPM. Visible="false">Previous< /asp: HyperLink> < ;asp: HyperLink ID="nextLink" runat="server" CssClass="PagingText" Visible="false">Next< /asp: HyperLink>