`
lovexuwenhao
  • 浏览: 197397 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

sqlhelper使用指南 之 使用 SqlHelper 类执行命令

阅读更多

摘要:Data Access Application Block 是一个 .NET 组件,包含优化的数据访问代码,可以帮助用户调用存储过程以及向 SQL Server 数据库发出 SQL 文本命令。它返回 SqlDataReader、DataSet 和 XmlReader 对象。您可以在自己的 .NET 应用程序中将其作为构造块来使用,以减少需要创建、测试和维护的自定义代码的数量。您可以下载完整的 C# 和 Visual Basic .NET 源代码以及综合文档。

简介

您是否正在从事 .NET 应用程序数据访问代码的设计和开发?您是否觉得自己总是在反复编写相同的数据访问代码?您是否曾经将数据访问代码包装在 Helper 函数中,以便能够在一行中调用存储过程?如果是,那么 Microsoft® Data Access Application Block for .NET 正是为您设计的。

Data Access Application Block 将访问 Microsoft SQL Server™ 数据库的性能和资源管理方面的最佳经验封装在一起。您可以很方便地在自己的 .NET 应用程序中将其作为构造块使用,从页减少了需要创建、测试和维护的自定义代码的数量。

尤其是,Data Access Application Block 可以帮助您:

  • 调用存储过程或 SQL 文本命令。
  • 指定参数详细信息。
  • 返回 SqlDataReader、DataSet 或 XmlReader 对象。

例如,在引用了 Data Access Application Block 的应用程序中,您可以简单地在一行代码中调用存储过程并生成 DataSet,如下所示:

 1[Visual Basic]
 2Dim ds As DataSet = SqlHelper.ExecuteDataset( _
 3      connectionString, _
 4      CommandType.StoredProcedure, _
 5      "getProductsByCategory", _
 6      new SqlParameter("@CategoryID", categoryID))
 7 
 8[C#]
 9DataSet ds = SqlHelper.ExecuteDataset( 
10      connectionString,
11      CommandType.StoredProcedure,
12      "getProductsByCategory",
13      new SqlParameter("@CategoryID", categoryID)); 


注意: Application Block for .NET(用于 .NET 的应用程序块)是基于对成功的 .NET 应用程序进行详细研究而设计的。它以源代码的形式提供,您可以原样使用,也可以针对自己的应用程序进行自定义。该应用程序块并不代表未来 Microsoft ADO.NET 程序库的发展方向。Microsoft ADO.NET 程序库是为在各种使用情况下实现对数据访问行为的精确控制而建立的。将来的 ADO.NET 版本可能会使用不同的模型来实现这个方案

SqlHelper 类提供了一组静态方法,可以用来向 SQL Server 数据库发出许多各种不同类型的命令。

SqlHelperParameterCache 类提供命令参数缓存功能,可以用来提高性能。该类由许多 Execute 方法(尤其是那些只运行存储过程的重写方法)在内部使用。数据访问客户端也可以直接使用它来缓存特定命令的特定参数集。

使用 SqlHelper 类执行命令

SqlHelper 类提供了五种 Shared (Visual Basic) 或 static (C#) 方法,它们是:ExecuteNonQueryExecuteDatasetExecuteReaderExecuteScalarExecuteXmlReader。实现的每种方法都提供一组一致的重载。这提供了一种很好的使用 SqlHelper 类来执行命令的模式,同时为开发人员选择访问数据的方式提供了必要的灵活性。每种方法的重载都支持不同的方法参数,因此开发人员可以确定传递连接、事务和参数信息的方式。类中实现的所有方法都支持以下重载:

 1[Visual Basic]
 2Execute* (ByVal connection As SqlConnection, _
 3          ByVal commandType As CommandType, _
 4          ByVal CommandText As String)
 5
 6Execute* (ByVal connection As SqlConnection, _
 7          ByVal commandType As CommandType, _
 8          ByVal commandText As String, _
 9          ByVal ParamArray commandParameters() As SqlParameter)
10
11Execute* (ByVal connection As SqlConnection, _
12          ByVal spName As String, _
13          ByVal ParamArray parameterValues() As Object)
14
15Execute* (ByVal transaction As SqlTransaction, _
16          ByVal commandType As CommandType, _
17          ByVal commandText As String)
18
19Execute* (ByVal transaction As SqlTransaction, _
20          ByVal commandType As CommandType, _
21          ByVal commandText As String, _
22          ByVal ParamArray commandParameters() As SqlParameter)
23
24Execute* (ByVal transaction As SqlTransaction, _
25          ByVal spName As String, _
26          ByVal ParamArray parameterValues() As Object)
27
28[C#]
29Execute* (SqlConnection connection, CommandType commandType, 
30          string commandText)
31
32Execute* (SqlConnection connection, CommandType commandType,
33          string commandText, params SqlParameter[] commandParameters)
34
35Execute* (SqlConnection connection, string spName, 
36          params object[] parameterValues)
37
38Execute* (SqlConnection connection, 
39          CommandType commandType, string commandText)
40
41Execute* (SqlConnection connection,
42          CommandType commandType, string commandText, 
43          params SqlParameter[] commandParameters)
44
45Execute* (SqlConnection connection,
46          string spName, params object[] parameterValues)
47

除这些重载以外,除 ExecuteXmlReader 之外的其他方法还提供了另一种重载:允许将连接信息作为连接字符串而不是连接对象来传递,如下面的方法签名所示:

[Visual Basic]
Execute
* (ByVal connectionString As String, _
          ByVal commandType As CommandType, _
          ByVal commandText As String)

Execute
* (ByVal connectionString As String, _
          ByVal commandType As CommandType, _
          ByVal commandText As String, _
          ByVal ParamArray commandParameters() As SqlParameter)

Execute
* (ByVal connectionString As String, _
          ByVal spName As String, _
          ByVal ParamArray parameterValues() As Object)

[C#]
Execute
* (string connectionString, CommandType commandType, 
          
string commandText)

Execute
* (string connectionString, CommandType commandType, 
          
string commandText, 
          
params SqlParameter[] commandParameters)

Execute
* (string connectionString, string spName, 
          
params object[] parameterValues)
注意: ExecuteXmlReader 不支持连接字符串,因为:与 SqlDataReader 对象不同,XmlReader 对象在 XmlReader 关闭时没有提供自动关闭连接的方法。如果客户端传递了连接字符串,那么当客户端完成对 XmlReader 的操作后,将无法关闭与 XmlReader 相关联的连接对象。

通过参考 Data Access Application Block 程序集并导入 Microsoft.ApplicationBlocks.Data 命名空间,您可以轻松编写使用任何一种 SqlHelper 类方法的代码,如下面的代码示例所示:

[Visual Basic]
Imports Microsoft.ApplicationBlocks.Data

[C#]
using Microsoft.ApplicationBlocks.Data;

导入命名空间后,您可以调用任何 Execute* 方法,如下面的代码示例所示:

[Visual Basic]
Dim ds As DataSet 
= SqlHelper.ExecuteDataset( _
   
"SERVER=(local);DATABASE=Northwind;INTEGRATED SECURITY=True;",
     _
  CommandType.Text, 
"SELECT * FROM Products")

[C#]
DataSet ds 
= SqlHelper.ExecuteDataset( 
   
"SERVER=DataServer;DATABASE=Northwind;INTEGRATED
     SECURITY=sspi;", _
   CommandType.Text, "SELECT * FROM Products");

 

 

转载地址:http://www.cnblogs.com/lc329857895/archive/2007/10/09/918603.html

分享到:
评论

相关推荐

    sqlhelper使用指南[文].pdf

    sqlhelper使用指南[文].pdf

    最新强大而简便的SqlHelper类

    3,使用前请先执行以下方法,以进行类初始化: string DBConnectionString = "data source=.;database=Northwind;user id=sa;pwd=''"; SqlHelper.Initialize("SqlClient", DBConnectionString); 4,SQL命令有参数时,...

    SQLHelper类的使用

    教你.net中SQLHelper类的使用

    Sqlhelper类的的内容

    Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容

    C# SqlHelper类 (微软官方)

    微软官方C# SqlHelper类 ,内带有注释说明,供大家参考

    SqlHelper.cs 微软的SQLHelper类(含完整中文注释)

    SqlHelper.cs 微软的SQLHelper类 微软的SQLHelper类(含完整中文注释) 非常好的SQLHelper

    非常实用的SqlHelper类

    本SqlHelper类(在ZDevTools.Data命名空间中)最与众不同的地方在于兼顾了易用性与灵活性:她通过委托的方式将你要执行的操作传入SqlHelper类中,所有的连接操作自动维护,却能将DBDataReader、DBCommand这样功能...

    Visual C++源代码 114 如何使用SqlHelper查询数据库记录

    Visual C++源代码 114 如何使用SqlHelper查询数据库记录Visual C++源代码 114 如何使用SqlHelper查询数据库记录Visual C++源代码 114 如何使用SqlHelper查询数据库记录Visual C++源代码 114 如何使用SqlHelper查询...

    sqlhelper类,sqlhelper类,

    sqlhelper类,可以在做项目时参考,这个类里没有什么错误,适合.net+SQL

    SqlHelper源码及使用实例

    SqlHelper源码及使用实例 SqlHelper源码及使用实例

    Visual C++源代码 115 如何使用SqlHelper增加数据库记录

    Visual C++源代码 115 如何使用SqlHelper增加数据库记录Visual C++源代码 115 如何使用SqlHelper增加数据库记录Visual C++源代码 115 如何使用SqlHelper增加数据库记录Visual C++源代码 115 如何使用SqlHelper增加...

    韩顺平sqlhelper工具类

    韩顺平sqlhelper工具类,一直贯穿于java jsp servlet javaweb中

    SqlHelper资料大全(强烈推荐)

    微软SqlHelper中文版 SqlHelper使用指南 SqlHelper的使用实例

    SqlHelper类

    SqlHelper类。内置了常用的操作数据库的方法,非常实用的类。平时开发就可以省略好多SQL语句。注:C#写的

    sqlhelper类

    sqlhelper 数据库连接的一个关键类

    Visual C++源代码 116 如何使用SqlHelper获取聚合函数值

    Visual C++源代码 116 如何使用SqlHelper获取聚合函数值Visual C++源代码 116 如何使用SqlHelper获取聚合函数值Visual C++源代码 116 如何使用SqlHelper获取聚合函数值Visual C++源代码 116 如何使用SqlHelper获取...

    SQLHelper数据库操作组件

    这提供了一种很好的使用 SqlHelper 类来执行命令的模式,同时为开发人员选择访问数据的方式提供了必要的灵活性。每种方法的重载都支持不同的方法参数,因此开发人员可以确定传递连接、事务和参数信息的方式。

    微软官方sqlhelper类

    微软官方的sqlhelper类,包含访问sqlserver和access的类,可以编译作为类库引用,也可以直接以代码形式加入项目,对于直接使用ADO.net访问数据库的小型项目来说,非常适用,大大减少了访问数据库的代码量。...

    C#公共类SQLHelper

    详细的SQLhelper类!使用在数据链路层减少了大量的代码工作!

Global site tag (gtag.js) - Google Analytics