您当前的位置:中客资源站网络学院.NET专区综合应用 → 文章内容 退出登录 用户管理
本类热门文章
相关下载
.NET 数据访问架构指南
作者:中客资源  来源:中客资源  发布时间:2007-2-7 2:48:45

减小字体 增大字体

m. 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的结果设置,它

上一页  [1] [2] [3] [4] [5] [6]  下一页

[] [返回上一页] [打 印]
文章评论 (评论内容只代表网友观点,与本站立场无关!)

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码