惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
博客园_首页
量子位
博客园 - 司徒正美
罗磊的独立博客
腾讯CDC
IT之家
IT之家
S
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
L
Lohrmann on Cybersecurity
H
Hacker News: Front Page
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
大猫的无限游戏
大猫的无限游戏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
I
InfoQ
T
Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
L
LINUX DO - 热门话题
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
Latest news
Latest news
Forbes - Security
Forbes - Security
Security Latest
Security Latest
NISL@THU
NISL@THU
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
The Hacker News
The Hacker News
M
MIT News - Artificial intelligence
S
SegmentFault 最新的问题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
B
Blog
A
Arctic Wolf
C
Check Point Blog
G
Google Developers Blog
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
L
LINUX DO - 最新话题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Privacy International News Feed
小众软件
小众软件

博客园 - hulu

从一个int值显示相应枚举类型的名称或者描述 第8章 商品目录管理 第2,第三章 概要 Scott Mitchell的ASP.NET2.0数据指南中文版索引 如何自定义gridview的表格显示? 7/14 原型编码阶段里程碑完成 asp.net2.0 的 gridview 7/9 原型编码阶段: (6) 使用DetailsView和FormView(7-5 11:54) - hulu 7/5 原型编码阶段: (5) 使用ADO.NET 7/3 原型编码阶段: (4) GridView的编辑更新 成功软件开发者的9种编程习惯(一) 6/29 原型编码阶段:(3) CommandField,TemplateField 6/29 原型编码阶段:(2) GridView的数据库操作 6/29 原型编码阶段:(1)连接并访问数据库 6/29 项目目录结构有所调整 6/28 项目原型前期实现任务确定 C# 2.0 除了泛型 C# 2.0 泛型
第4章 创建商品目录
hulu · 2008-12-29 · via 博客园 - hulu

演示了 新表的创建,主键的创建,外部关键字的约束,利用数据库关系图来显示和管理外键的约束,实现多对多的关系。

利用存储过程在数据层实现分页,这是性能最高的一种分页方式,原理如下:

在SQL2005中支持对结果集的行进行编号,这样我们要取出结果中6到10条的记录就可以这样写:

--申明临时表变量@Products
declare @Products TABLE
(RowID int,
ProductID int,
Name nvarchar(50),
Description nvarchar(1000))

--向临时表赋值
insert into @Products
select row_number() over (order by productid) as rowid, productid,name,description from product

--从临时表中取数据
select productid,Name,Description FROM @Products
where rowid>=6 and rowid<=10

实际应用在获取分类中商品的存储过程代码如下:

create procedure GetProductInCategory
(@CategoryID INT,
@DescriptionLength INT,
@PageNumber INT,
@ProductsPerPage INT,
@HowManyProducts INT OUTPUT)
AS 

DECLARE @Products TABLE
(RowID INT,
ProductID INT,
Name NVARCHAR(

50),
Description NVARCHAR(
1000),
Price Money,
Img1File NVARCHAR(
50),
Img2File NVARCHAR(
50),
OnSub1Promotion bit,
OnCatalogPromotion bit
)

INSERT INTO @Products
SELECT ROW_NUMBER() OVER (ORDER BY Product.ProductID),Product.ProductID,Name,SUBSTRING(Description,

1,@DescriptionLength) +'' AS Description,
Price,Img1File,Img2File,OnSub1Promotion,OnCatalogPromotion FROM Product INNER JOIN ProductCategory
ON Product.ProductID
=ProductCategory.ProductID
WHERE ProductCategory.CategoryID
=@CategoryID

SELECT @HowManyProducts 

= COUNT(ProductID) FROM @Products

SELECT ProductID,Name,Description,Price,Img1File,Img2File,OnSub1Promotion,OnCatalogPromotion
FROM @products
WHERE RowID

>(@PageNumber-1)*@ProductsPerPage AND RowID<=@PageNumber*@ProductsPerPage

获取某个大类下推荐的商品列表的存储过程GetProductOnSub1Promotion:

create procedure GetProductOnSub1Promotion
(@Sub1ID INT,
@DescriptionLength INT,
@PageNumber INT,
@ProductsPerPage INT,
@HowManyProducts INT OUTPUT)
AS 

DECLARE @Products TABLE
(RowID INT,
ProductID INT,
Name NVARCHAR(

50),
Description NVARCHAR(
1000),
Price Money,
Img1File NVARCHAR(
50),
Img2File NVARCHAR(
50),
OnSub1Promotion bit,
OnCatalogPromotion bit
)

INSERT INTO @Products
SELECT ROW_NUMBER() OVER (ORDER BY ProductID) AS Row,ProductID,Name,SUBSTRING(Description,

1,@DescriptionLength) +'' AS Description,
Price,Img1File,Img2File,OnSub1Promotion,OnCatalogPromotion FROM 
(SELECT DISTINCT Product.ProductID,Product.Name,SUBSTRING(Product.Description,
1,@DescriptionLength)+'' AS Description,Price,Img1File,Img2File,OnSub1Promotion,OnCatalogPromotion
FROM Product INNER JOIN ProductCategory ON Product.ProductID
=ProductCategory.ProductID
INNER JOIN Category ON ProductCategory.CategoryID
=Category.CategoryID
WHERE Product.OnSub1Promotion
=1
AND Category.Sub1ID
=@Sub1ID)
AS ProductOnSub1Pr

SELECT @HowManyProducts 

= COUNT(ProductID) FROM @Products

SELECT ProductID,Name,Description,Price,Img1File,Img2File,OnSub1Promotion,OnCatalogPromotion
FROM @products
WHERE RowID

>(@PageNumber-1)*@ProductsPerPage AND RowID<=@PageNumber*@ProductsPerPage

嗯 这样的支持分页的存储过程代码性能很不错 不过只有SQL2005支持。接下来是业务层的编码, 其功能呢个只是调用数据层的存储过程。里面提到了使用结构类型,他是一个值类型,比引用类型如类效率要高。

表示层主要是编写用户控件,这里要注意的是,控件里如果是用的ASP.NET语法,则需要使用相对路径,因为控件是存放在/UserControls子目录下的,如

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl='<%# "../Catalog.aspx?Sub1ID="+Request.QueryString["Sub1ID"]+"&CategoryID=" + Eval("CategoryID") %>'

但如果是使用HTML语法,就不需要使用相对路径了,可以理解为是类似于shtml方式的<!--#include file=""-->嵌入,比如:

<asp:DataList ID="list" Runat="server" RepeatColumns="2">
<ItemTemplate>
<class="ProductName" href='Product.aspx?ProductID=<%# Eval("ProductID")%> '><%# Eval("Name"%></a>

在表示层中最大的挑战是实现翻页链接,为了获取URL中QueryString的参数并且再加上page页码的参数组合成新的url查询参数,程序引用了

using System.Collections.Specialized;

对前一页PreLink和NextLink的链接赋值如下:

        if (howManyPages > 1)
        {
            
int currPage = Int32.Parse(page);
            pagingLabel.Visible 
= true;
            preLink.Visible 
= true;
            nextLink.Visible 
= true;
            pagingLabel.Text 
= "" + page + "页,共" + howManyPages.ToString() + "";
            
if (currPage == 1)
                preLink.Enabled 
= false;
            
else
            {
                NameValueCollection query 
= Request.QueryString;
                
string paramName, newQueryString = "?";
                
for (int i = 0; i < query.Count; i++)
                    
if (query.AllKeys[i] != null)
                        
if ((paramName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                            newQueryString 
+= paramName + "=" + query[i] + "&";
                preLink.NavigateUrl 
= Request.Url.AbsolutePath + newQueryString + "Page=" + (currPage - 1).ToString();
            }
if (currPage == howManyPages)
                nextLink.Enabled 
= false;
            
else
            {
                NameValueCollection query 
= Request.QueryString;
                
string paramName, newQueryString = "?";
                
for (int i = 0; i < query.Count; i++)
                    
if (query.AllKeys[i] != null)
                        
if ((paramName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                            newQueryString 
+= paramName + "=" + query[i] + "&";
                nextLink.NavigateUrl 
= Request.Url.AbsolutePath + newQueryString + "Page=" + (currPage + 1).ToString();
            }
        }