5.ExecuteXmlReader。此方法返回 FOR XML 查询的 XML 片段。
VB.NET版实例(SqlHelper类):
Imports System.Data.SqlClient Imports System.Reflection Imports System.Data Imports System.Configuration Imports System.Collections.Generic Public Class SQLHelper '定义一个数据库连接对象 Dim strCon As String = ConfigurationSettings.AppSettings("ConnString") Dim conn As SqlConnection = New SqlConnection(strCon) '定义一个命令对象 Dim cmd As New SqlCommand ''' <summary> ''' 关闭连接 ''' </summary> ''' <param name="conn">需要关闭的连接</param> ''' <remarks></remarks> Private Sub CloseConn(ByVal conn As SqlConnection) '如果没有关闭,则关闭连接 If (conn.State <> ConnectionState.Closed) Then conn.Close() conn = Nothing End If End Sub ''' <summary> ''' 关闭命令 ''' </summary> ''' <param name="cmd">需要关闭的命令</param> ''' <remarks></remarks> Private Sub CloseCmd(ByVal cmd As SqlCommand) '如果没有关闭命令,则关闭命令 If Not IsNothing(cmd) Then cmd.Dispose() '处理 ' cmd = Nothing End If End Sub ''' <summary> ''' 有参数的 增删改 操作 ''' </summary> ''' <param name="cmdText">需要执行的命令</param> ''' <param name="cmdType">所执行命令的类型,一般是sql语句,也有可能是存储过程,或表</param> ''' <param name="sqlParams">参数数组</param> ''' <returns>返回执行 增删改 语句受影响的行数,为Integer类型</returns> ''' <remarks></remarks> Public Function ExecuteNoQuery(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As Integer '将传入的值,分别为cmd的属性赋值 cmd.Parameters.AddRange(sqlParams) '传入参数 cmd.CommandType = cmdType cmd.Connection = conn '设置连接 cmd.CommandText = cmdText 'cmd = New SqlCommand(cmdText, conn) Dim result As Integer '执行操作 Try conn.Open() result = cmd.ExecuteNonQuery() '执行增删改操作并返回受影响的行数 cmd.Parameters.Clear() '清除参数 Catch ex As Exception MsgBox(ex.Message, , "数据库操作") Finally Call CloseConn(conn) '关闭连接 Call CloseCmd(cmd) '关闭命令 End Try Return result End Function ''' <summary> ''' 无参数的 增删改 操作 ''' </summary> ''' <param name="cmdText">需要执行的命令</param> ''' <param name="cmdType">所执行的命令的类型,一般是sql语句,也有可能是存储过程,或表</param> ''' <returns>返回执行 增删改 语句受影响的行数,为Integer类型</returns> ''' <remarks></remarks> Public Function ExecuteNoQuery(ByVal cmdText As String, ByVal cmdType As CommandType) As Integer '将传入的参数分别为cmd属性赋值 cmd.CommandType = cmdType cmd.Connection = conn cmd.CommandText = cmdText '设置查询单的语句 'cmd = New SqlCommand(cmdText, conn) Dim res As Integer '执行操作 Try conn.Open() res = cmd.ExecuteNonQuery() '执行 增删改操作并返回收到影响的行术 cmd.Parameters.Clear() '清楚参数 Catch ex As Exception MsgBox(ex.Message, , "数据库操作") Finally Call CloseConn(conn) Call CloseCmd(cmd) End Try Return res End Function ''' <summary> ''' 有参数的查询操作 ''' </summary> ''' <param name="cmdText">需要执行的命令</param> ''' <param name="cmdType">所执行命令的类型,一般是sql语句,也有可能是存储过程或者表</param> ''' <param name="sqlParams">参数数组</param> ''' <returns>返回执行 查询得到的结果,为DataTable类型</returns> ''' <remarks></remarks> Public Function ExecSelect(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable Dim sqlAdapter As New SqlDataAdapter '声明适配器 Dim dt As New DataTable '声明数据表 Dim ds As New DataSet '声明数据缓存 cmd.CommandType = cmdType cmd.Connection = conn cmd.CommandText = cmdText '设置查询语句 '将传入的值分别付给cmd的属性 cmd.Parameters.AddRange(sqlParams) '将参数传入 'cmd = New SqlCommand(cmdText, conn) sqlAdapter = New SqlDataAdapter(cmd) '实例化适配器 '执行操作 Try sqlAdapter.Fill(ds) '用适配器对ds进行填充 dt = ds.Tables(0) '返回数据集的第一个表 cmd.Parameters.Clear() '清楚参数 Catch ex As Exception MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告") Finally Call CloseConn(conn) Call CloseCmd(cmd) End Try Return dt End Function ''' <summary> ''' 无参数的查询操作 ''' </summary> ''' <param name="cmdText">需要执行的命令</param> ''' <param name="cmdType">所执行命令的类型,一般是sql语句,也有可能是存储过程或者表</param> ''' <returns>返回执行 查询得到的结果,为DataTable类型</returns> ''' <remarks></remarks> Public Function ExecSelect(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTable Dim sqlAdapter As SqlDataAdapter '声明适配器 Dim dt As New DataTable '声明数据表 Dim ds As New DataSet '声明数据缓存 '将传入的值分别给cmd属性赋值 cmd.CommandType = cmdType '设置一个值,届时cmdtext cmd.Connection = conn cmd.CommandText = cmdText sqlAdapter = New SqlDataAdapter(cmd) '实例化适配器 '执行操作 Try sqlAdapter.Fill(ds) '用适配器对ds进行填充 dt = ds.Tables(0) '返回数据集的第一个表 Catch ex As Exception MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告") Finally Call CloseConn(conn) Call CloseCmd(cmd) End Try Return dt End Function End Class
D层调用SqlHelper类中对应方法的实例:
<span style="font-family:KaiTi_GB2312;font-size:18px;"> '''调用有参数的增删改操作 ''' <summary> ''' 向基本数据设定表中插入一行信息 ''' </summary> ''' <param name="enBasicData">传入BasicData表中属性的值</param> ''' <returns>返回布尔值</returns> ''' <remarks></remarks> Public Function InsertBasicData(enBasicData As BasicDataEntity) As Boolean Implements IDAL.IBasicData.InsertBasicData '定义数据库连接字符串 Dim sql As String = "Insert into T_BasicData (Rate,TmpRate,UnitTime,LeastTime,PrepareTime,LimitCash,Date,Time,UserID) value(@rate,@tmprate,@unittime,@leasttime,@reparetime,@limitcash,@date,@time,@userid)" '提出要插入的字符串,并赋值 Dim sqlparams As SqlParameter() = {New SqlParameter("@rate", enBasicData.Rate), New SqlParameter("@tmprate", enBasicData.TmpRate), New SqlParameter("@unittime", enBasicData.UnitTime), New SqlParameter("@leasttime", enBasicData.LeastTime), New SqlParameter("@preparetime", enBasicData.PrepareTime), New SqlParameter("@limitcash", enBasicData.LimitCash), New SqlParameter("@date", enBasicData.zDate), New SqlParameter("@time", enBasicData.Time), New SqlParameter("@userid", enBasicData.UserID)} '实例化一个sqlhelper类的对象 Dim helper As New SQLHelper '调用sqlhelper类的方法 Dim InsertOK = helper.ExecuteNoQuery(sql, CommandType.Text, sqlparams) Return InsertOK End Function '''无参数的增删改操作 ''' <summary> ''' 通过用户账号删除用户记录表中的所有信息 ''' </summary> ''' <returns>返回布尔值</returns> ''' <remarks></remarks> Public Function DeleteByUserID(enUser As UserEntity) As Boolean Implements IDAL.IUser.DeleteByUserID '建立删除连接数据库语句 Dim sql As String = "delete from T_User" '定义一个sqlHelper类的对象 Dim helper As New SQLHelper '调用sqlHelper中的删除方法 Dim DeleteOK = helper.ExecuteNoQuery(sql, CommandType.Text) Return DeleteOK End Function '''调用有参数的查询操作 ''' <summary> ''' 通过卡号查询卡表中的信息 ''' </summary> ''' <param name="enCard">传入CardID的值</param> ''' <returns>返回泛型集合</returns> ''' <remarks></remarks> Public Function SelectByCardID(enCard As CardEntity) As List(Of CardEntity) Implements IDAL.ICard.SelectByCardID '定义数据库连接字符串 Dim sql As String = "Select * from T_Card where CardID=@cardid" '提出查询的字符串,并赋值 Dim sqlparams As SqlParameter() = {New SqlParameter("@cardid", enCard.CardID)} '实例化一个临时表 Dim table As New DataTable '实例化一个sqlhelper类对象 Dim helper As New SQLHelper '调用sqlhelper类的查询方法,并将查询到的信息放到临时表中 table = helper.ExecSelect(sql, CommandType.Text, sqlparams) '实例化一个泛型集合 Dim mylist As New List(Of CardEntity) '将临时表转化为泛型集合 mylist = DataTolist.converToList(Of CardEntity)(table) Return mylist End Function '''调用无参数的查询操作 ''' <summary> ''' 查询基本数据设定表中的最新信息 ''' </summary> ''' <returns>返回泛型集合</returns> ''' <remarks></remarks> Public Function SelectBasicData() As List(Of BasicDataEntity) Implements IDAL.IBasicData.SelectBasicData '定义数据库连接字符串 Dim sql As String = "Select top 1 * from T_BasicData order by Date desc" '实例化临时表 Dim table As New DataTable '实例化sqlhelper类对象 Dim helper As New SQLHelper '调用sqlhelper类的查询方法,将查询到的信息放到临时表中 table = helper.ExecSelect(sql, CommandType.Text) '实例化一个泛型集合对象 Dim mylist As New List(Of BasicDataEntity) '将临时表转化为泛型集合 mylist = DataTolist.converToList(Of BasicDataEntity)(table) Return mylist End Function</span>
SqlHelper真的很有用,期待与您下次更亲密的约会。