网站首页站长博客下载中心域名交易站长论坛域名主机免费电邮免费域名中文排行排名查询站长书库书籍教程下载
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
学院首页 网络编程 网页设计 图形图象 数 据 库 服 务 器 网络媒体 网络安全 个人专栏 站长CLUB 业界新闻 信息公告
 当前位置:首页 >> 网络编程 >> NET专区 >> 正文
公告通知
返回上级列表
资料搜索
相关文章
用户自定义控件的应用
c#.net常用函数和方法集
在VB中使用水晶报表的一种简易编
C#调用父类的父类的方法
浏览.NET Framework 2.0 类型库中
为.Text Blog 添加 计数器
编程实现邮件地址有效性检测 
VB/VB.NET/C#导出到Excel的方法
c#高性能在WEB端产生验证图片
用System.Web.Caching.Cache保存
.NET 数据访问架构指南
[ 来源:CSDN | 作者:无从考证 | 时间:2005-11-11 14:11:55 | 浏览:人次 ]
收藏到新浪ViVi 收藏到365KEY 收藏到我摘  字号选择〖    〗/ 双击滚屏 单击停止  
扩展性要求 
性能要求 
网络带宽 
数据库服务器的存储器和功率 
中级服务器的存储器和功率 
由分页查询所返回的行数 
数据总页数的大小 
性能测试表明利用存储过程的手工方法在很大的应力水平范围上都提供了最佳性能。然而,由于手工方法在服务器上执行工作,如果大部分站点功能都依赖数据分页功能,那么服务器性能就会成一个关键要素。为确保这种方法能适合特殊环境,应该测试各种特殊要求的选项。 
下面将讨论各种不同的选项。 
使用SqlDataAdapter 
如前面所讨论的,SqlDataAdapter是用来把来自数据库的数据填充到DataSet中,过载的Fill方法中的任一个都需要两个整数索引值(如下列代码所示): 
public int Fill( 
DataSet dataSet, 
int startRecord, 
int maxRecords, 
string srcTable 
); 
StartRecord值标示从零开始的记录起始索引值。MaxRecord值表示从startRecord开始的记录数,并将拷贝到新的DataSet中。 
SqlDataAdapter在内部利用SqlDataReader执行查询并返回结果。SqlDataAdapter读取结果并创建基于来自SalDataReader的数据的Dataset。SqlDataAdapter通过startRecord和maxRecords把所有结果都拷贝到新生成的DataSet中,并丢弃不需要的数据。这意味着许多不必要的数据将潜在的通过网络进入数据访问客户--这是这种方法的主要缺陷。 
比如,如果有1000个记录,而需要的是第900到950个记录,那么前面的899个记录将仍然穿越网络然后被丢弃。对于小数量的记录,这种开销可能是比较小的,但如果针对大量数据的分页,则这种开销就会非常巨大。 
使用ADO 
实现分页的另一个选项是利用基于COM的ADO进行分页。这种方法的目标是获得访问服务器光标。服务器光标通过ADO Recordset对象显示。可以把Recordset光标的位置设置到adUseServer中。如果你的OLE DB供应器支持这种设置(如SQLOLEDB那样),就可以使用服务器光标。这样就可以利用光标直接导航到起始记录,而不需要将所有数据传过网络进入访问数据的用户代码中。 
这种方法有下面两个缺点: 
在大多数情况下,可能需要将返回到Recordset对象中的记录翻译成DataSet中的内容,以便在客户管理的代码中使用。虽然OleDbDataAdapter确实在获取ADO Recordset对象并把它翻译成Dataset时过载了Fill方法,但是并没有利用特殊记录进行开始与结束操作的功能。唯一现实的选项是把开始记录移动到Recordset对象中,循环每个记录,然后手工拷贝数据到手工生成的新Dataset中。这种操作,尤其是利用COM Interop调用,其优点可能不仅仅是不需要在网络上传输多余的数据,尤其对于小的DataSet更明显。 
从服务器输出所需数据时,将保持连接和服务器光标开放。在数据库服务器上,光标的开放与维护需要昂贵的资源。虽然该选项提高了性能,但是由于为延长的时间两消耗服务器资源,从而也有可能降低可扩展性。 
提供手工实现 
在本部分中讨论的数据分页的最后一个选项是利用存储过程手工实现应用程序的分页功能。对于包含唯一关键字的表格,实现存储过程相对容易一些。而对于没有唯一关键字的表格(也不应该有许多关键字),该过程会相对复杂一些。 
带有唯一关键字的表格的分页 
如果表格包含一个唯一关键字,就可以利用WHERE条款中的关键字创建从某个特殊行起始的结果设置。这种方法,与用来限制结果设置大小的SET ROWCOUNT状态是相匹配的,提供了一种有效的分页原理。这一方法将在下面存储的代码中说明: 
CREATE PROCEDURE GetProductsPaged 
@lastProductID int, 
@pageSize int 
AS 
SET ROWCOUNT @pageSize 
SELECT * 
FROM Products 
WHERE [standard search criteria] 
AND ProductID > @lastProductID 
ORDER BY [Criteria that leaves ProductID monotonically increasing] 
GO 
这个存储过程的调用程序仅仅维护LastProductID的值,并通过所选的连续调用之间的页的大小增加或减小该值。 
不带有唯一关键字的表格的分页 
如果需要分页的表格没有唯一关键字,可以考虑添加一个--比如利用标识栏。这样就可以实现上面讨论的分页方案了。 
只要能够通过结合结果记录中的两个或更多区域来产生唯一性,就仍然有可能实现无唯一关键字表格的有效分页方案。 
比如,考察下列表格: Col1 Col2 Col3 Other columns… 
A 1 W … 
A 1 X . 
A 1 Y . 
A 1 Z . 
A 2 W . 
A 2 X . 
B 1 W … 
B 1 X . 

对于该表,结合Col 、Col2 和Col3就可能产生一种唯一性。这样,就可以利用下面存储过程中的方法实现分布原理: 
CREATE PROCEDURE RetrieveDataPaged 
@lastKey char(40), 
@pageSize int 
AS 
SET ROWCOUNT @pageSize 
SELECT 
Col1, Col2, Col3, Col4, Col1+Col2+Col3 As KeyField 
FROM SampleTable 
WHERE [Standard search criteria] 
AND Col1+Col2+Col3 > @lastKey 
ORDER BY Col1 ASC, Col2 ASC, Col3 ASC 
GO 
客户保持存储过程返回的keyField栏的最后值,然后又插入回到存储过程中以控制表的分页。 
虽然手工实现增加了数据库服务器上的应变,但它避免了在网络上传输不必要的数据。性能测试表明在整个应变水平中这种方法都工作良好。然而,根据站点工作所涉及的数据分页功能的多少,在服务器上进行手工分页可能影响应用程序的可扩展性。应该在所在环境中运行性能测试,为应用程序找到最合适的方法。 
附录 
如何为一个.NET类启用对象结构 
要利用Enterprise (COM+)Services为对象结构启用.NET管理的类,需要执行下列步骤: 
从位于System. Enterprise Services名字空间中的Serviced Component中导出所需类。 
using System.EnterpriseServices; 
public class DataAccessComponent : ServicedComponent 
为该类添加Construction Enabled属性,并合理地指定缺省结构字符串,该缺省值保存在COM+目录中,管理员可以利用组件服务微软管理控制台(MNC)的snap-in来维护该缺省值。 
[ConstructionEnabled(Default="default DSN")] 
public class DataAccessComponent : ServicedComponent 
提供虚拟Construct方法的替换实现方案。该方法在对象语言构造程序之后调用。在COM目录中保存的结构字符串是该方法的唯一字符串。 
public override void Construct( string constructString ) 

// Construct method is called next after constructor. 
// The configured DSN is supplied as the single argument 

通过Assembly key文件或Assembly key Name属性为该汇编提供一个强名字。任何用COM+服务注册的汇编必须有一个强名字。关于带有强名字汇编的更多信息,参考:http://msdn.microsoft.com/library/en-us/cpguide/html/cpconworkingwithstrongly- namedassemblies.Asp。 
[assembly: AssemblyKeyFile("DataServices.snk")] 
为支持动态注册,可以利用汇编层上的属性ApplicationName和Application Action分别指定用于保持汇编元素和应用程序动作类型的COM+应用程序的名字。关于汇编注册的更多信息,参考: http://msdn.microsoft.com/library/en-us/cpguide/html/cpconregisteringserviced components.asp。 
// the ApplicationName attribute specifies the name of the 
// COM+ Application which will hold assembly components 
[assembly : ApplicationName("DataServices")] 
// the ApplicationActivation.ActivationOption attribute specifies 
// where assembly components are loaded on activation 
// Library : components run in the creator’s process 
// Server : components run in a system process, dllhost.exe 
[assembly: ApplicationActivation(ActivationOption.Library)] 
下列代码段是一个叫做DataAccessComponent的服务组件,它利用COM+结构字符串来获得数据库连接字符串。 
using System; 
using System.EnterpriseServices; 
// the ApplicationName attribute specifies the name of the 
// COM+ Application which will hold assembly components 
[assembly : ApplicationName("DataServices")] 
// the ApplicationActivation.ActivationOption attribute specifies 
// where assembly components are loaded on activation 
// Library : components run in the creator’s process 
// Server : components run in a system process, dllhost.exe 
[assembly: ApplicationActivation(ActivationOption.Library)] 
// Sign the assembly. The snk key file is created using the 
// sn.exe utility 
[assembly: AssemblyKeyFile("DataServices.snk")] 
[ConstructionEnabled(Default="Default DSN")] 
public class DataAccessComponent : ServicedComponent 

private string connectionString; 
public DataAccessComponent() 

// constructor is called on instance creation 

public override void Construct( string constructString ) 

// Construct method is called next after constructor. 
// The configured DSN is supplied as the single argument 
this.connectionString = constructString; 


如何利用SqlDataAdapter来检索多个行 
下面的代码说明如何利用SqlDataAdapter对象发出一个生成Data Set或Datatable的命令。它从SQL Server Northwind数据库中检索一系列产品目录。 
using System.Data; 
using System.Data.SqlClient; 
public DataTable RetrieveRowsWithDataTable() 

using ( SqlConnection conn = new SqlConnection(connectionString) ) 

SqlCommand cmd = new SqlCommand("DATRetrieveProducts", conn); 
cmd.CommandType = CommandType.StoredProcedure; 
SqlDataAdapter da = new SqlDataAdapter( cmd ); 
DataTable dt = new DataTable("Products"); 
da.Fill(dt); 
return dt; 


按下列步骤利用SqlAdapter生成DataSet或DataTable: 
创建SqlCommand对象启用存储过程,并把它与SqlConnection对象(显示的)或连接字符串(未显示)相联系。 
创建一个新的SqlDataAdapter对象,并把它SqlCommand对象相联系。 
创建DataTable(或者DataSet)对象。利用构造程序自变量命名DataTable. 
调用SqlData Adapter对象的Fill方法,把检索的行转移到DataSet或Datatable中。 
如何利用SqlDataReader检索多个行 
下列代码说明了如何利用SqlDataReader方法检索多行: 
using System.IO; 
using System.Data; 
using System.Data.SqlClient; 
public SqlDataReader RetrieveRowsWithDataReader() 

SqlConnection conn = new SqlConnection( 
"server=(local);Integrated Security=SSPI;database=northwind"); 
SqlCommand cmd = new SqlCommand("DATRetrieveProducts", conn ); 
cmd.CommandType = CommandType.StoredProcedure; 
try 

conn.Open(); 
// Generate the reader. CommandBehavior.CloseConnection causes the 
// the connection to be closed when the reader object is closed 
return( cmd.ExecuteReader( CommandBehavior.CloseConnection ) ); 

catch 

conn.Close(); 
throw; 


// Display the product list using the console 
private void DisplayProducts() 

SqlDataReader reader = RetrieveRowsWithDataReader(); 
while (reader.Read()) 

Console.WriteLine("{0} {1} {2}", 
reader.GetInt32(0).ToString(), 
reader.GetString(1) ); 

reader.Close(); // Also closes the connection due to the 
// CommandBehavior enum used when generating the reader 

按下列步骤利用SqlDataReader检索多行: 
创建用于执行存储的过程的SqlCommand对象,并把它与SqlConnection对象相联系。 
打开链接。 
通过调用SqlCommand对象的Excute Reader方法生成SqlDataReader对象。 
从流中读取数据,调用SqlDataReader对象的Read方法来检索行,并利用分类的存取程序方法(如GetIut 32和Get String方法)检索列的值。 
完成读取后,调用Close方法。 
如何利用XmlReader检索多个行 
可以利用SqlCommand对象生成XmlReader对象,它提供对XML数据的基于流的前向访问。该命令(通常是一个存储的过程)必须生成一个基于XML的结果设置,它对于SQL Server2000通常是由带有有效条款FOR XML的SELECT状态组成。下列代码段说明了这种方法: 
public void RetrieveAndDisplayRowsWithXmlReader() 

SqlConnection conn = new SqlConnection(connectionString); 
SqlCommand cmd = new SqlCommand("DATRetrieveProductsXML", conn ); 
cmd.CommandType = CommandType.StoredProcedure; 
try 

conn.Open(); 
XmlTextReader xreader = (XmlTextReader)cmd.ExecuteXmlReader(); 
while ( xreader.Read() ) 

if ( xreader.Name == "PRODUCTS" ) 

string strOutput = xreader.GetAttribute("ProductID"); 
strOutput += " "; 
strOutput += xreader.GetAttribute("ProductName"); 
Console.WriteLine( strOutput ); 


xreader.Close(); 

catch 

throw; 

finally 

conn.Close(); 


上述代码使用了下列存储过程: 
CREATE PROCEDURE DATRetrieveProductsXML 
AS 
SELECT * FROM PRODUCTS 
FOR XML AUTO 
GO 
按下列步骤检索XML数据: 
创建SqlCommand对象启用生成XML结果设置的过程。(比如,利用SELECT状态中的FOR XML条款)。把SqlCommand对象与一个链接相联系。 
调用SqlCommand对象的ExecuteXmlReader方法,并把结果分配给前向对象XmlTextReader。当不需要任何返回数据的基于XML的验证时,这是应该使用的最快类型的XmlReader对象。 
利用XmlTextReader对象的Read方法读取数据。 
如何利用存储过程输出参数检索单个行 
可以调用一个存储过程,它通过一种称做输出参数的方式可以在单个行中返回检索数据项。下列代码段利用存储的过程检索产品的名称和单价,该产品包含在Northwind数据库中。 
void GetProductDetails( int ProductID, 
out string ProductName, out decimal UnitPrice ) 

SqlConnection conn = new SqlConnection( 
"server=(local);Integrated Security=SSPI;database=Northwind"); 
// Set up the command object used to execute the stored proc 
SqlCommand cmd = new SqlCommand( "DATGetProductDetailsSPOutput", conn ); 
cmd.CommandType = CommandType.StoredProcedure; 
// Establish stored proc parameters. 
// @ProductID int INPUT 
// @ProductName nvarchar(40) OUTPUT 
// @UnitPrice money OUTPUT 
// Must explicitly set the direction of output parameters 
SqlParameter paramProdID = 
cmd.Parameters.Add( "@ProductID", ProductID ); 
paramProdID.Direction = ParameterDirection.Input; 
SqlParameter paramProdName = 
cmd.Parameters.Add( "@ProductName", SqlDbType.VarChar, 40 ); 
paramProdName.Direction = ParameterDirection.Output; 
SqlParameter paramUnitPrice = 
cmd.Parameters.Add( "@UnitPrice", SqlDbType.Money ); 
paramUnitPrice.Direction = ParameterDirection.Output; 
try 

conn.Open(); 
// Use ExecuteNonQuery to run the command. 
// Although no rows are returned any mapped output parameters 
// (and potentially return values) are populated 
cmd.ExecuteNonQuery( ); 
// Return output parameters from stored proc 
ProductName = paramProdName.Value.ToString(); 
UnitPrice = (decimal)paramUnitPrice.Value; 

本新闻共3页,当前在第2页  1  2  3  


[发送给好友]  [打印本页]  [关闭窗口]  [返回顶部]   转载请注明来源:http://edu.chinaz.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
栏目编辑: 设计风 责任编辑: 简若宁
原始作者: 无从考证 录入时间: 2005-11-11 14:11:55
信息来源: CSDN 投稿信箱: Edu#chinaz.com
设为首页 - 加入收藏 - 关于我们 - 广告服务 - 版权申明 - 友情链接 - 联系方式 - 总编信箱 - 会员投稿