上面说了SqlHelper的简介,接下来介绍它具体的运行机制。首先和普通连接数据库的步骤一样,要先定义Connection对象,给它相应的连接字符串,连接字符串在开发项目时我们一般都是用反射来配置,然后用Command对象来对数据库执行操作。其实SqlHelper类中的几个方法就是command对象的几个方法,根据需求,我们一般用两个,一个是执行增删改的ExecuteNonQuery()和执行查询的ExecuteReader()。
ExecuteNonQuery()的返回值为int型,返回受影响的行数
ExecuteReader()返回的是一个结果集DataTable,它是一个网络虚拟表。
根据项目需要有时还会用到ExecuteScalar、ExecuteXmlReader等方法。
同时还会用到command的一些参数对象及其方法:
SqlParameter:是Sqlcommand的一个参数对象
AddRange:添加多个SqlParameter。
CommandText:获取或设置要对数据源执行的Transact—SQL语句或存储过程
CommandType:获取或设置一个值,该值指示如何解释CommandText属性
我们通常把这些方法分为有参和无参两种情况
Imports System.Data.SqlClient Imports System.Configuration Public Class sqlHelper '有参数的增、删、改操作 Public Function ExecuteNoQuery(cmdText As String, cmdType As CommandType, paras As SqlParameter()) As Integer Dim ConnStr As String = System.Configuration.ConfigurationManager.AppSettings("ConnStr") Dim conn As SqlConnection = New SqlConnection(ConnStr) '定义一个命令对象 Dim cmd As New SqlCommand cmd = New SqlCommand(cmdText, conn) '或用cmd =conn.createcommand() 'cmd.Parameters.AddRange(paras) cmd.Parameters.AddRange(paras) '添加参数 Dim res As Integer Try '打开数据库 If conn.State = ConnectionState.Closed Then conn.Open() End If res = cmd.ExecuteNonQuery() Catch ex As Exception MsgBox(ex.Message, "数据库打开错误") Finally '关闭数据库 If conn.State = ConnectionState.Open Then conn.Close() End If End Try Return res End Function '无参数的增删改操作 Public Shared Function ExecuteNoQuery(cmdText As String, cmdType As CommandType) As Integer Dim ConnStr As String = System.Configuration.ConfigurationManager.AppSettings("ConnStr") Dim conn As SqlConnection = New SqlConnection(ConnStr) Dim cmd As New SqlCommand cmd.CommandType = cmdType cmd = New SqlCommand(cmdText, conn) Dim res As Integer '不加参数 Try If conn.State = ConnectionState.Closed Then conn.Open() End If res = cmd.ExecuteNonQuery() Catch ex As Exception MsgBox(ex.Message, "数据库操作") Finally '关闭数据库 If conn.State = ConnectionState.Open Then conn.Close() End If End Try '返回受影响的行数 Return res End Function '有参数的查询操作 Public Function GetDataTable(ByVal cmdTxt As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As DataTable Dim strConnStr As String = System.Configuration.ConfigurationManager.AppSettings("ConnStr") '定义一个数据库连接对象 Dim conn As SqlConnection = New SqlConnection(strConnStr) '定义一个命令对象 Dim cmd As New SqlCommand Dim adataset As DataSet '定义一个适配器对象 Dim adaptor As SqlDataAdapter cmd = New SqlCommand(cmdTxt, conn) adaptor = New SqlDataAdapter(cmd) adataset = New DataSet cmd.Parameters.AddRange(paras) Try If conn.State = ConnectionState.Closed Then conn.Open() End If adaptor.Fill(adataset) '填充数据集 'Return adataset.Tables() Catch ex As Exception MsgBox(ex.Message, , "数据库打开错误") Finally '关闭数据库连接 If conn.State = ConnectionState.Open Then conn.Close() End If End Try Return adataset.Tables(0) End Function '无参数的查询 Public Function GetDataTable(ByVal cmdTxt As String, ByVal cmdType As CommandType) As DataTable Dim strConnStr As String = System.Configuration.ConfigurationManager.AppSettings("ConnStr") Dim conn As SqlConnection = New SqlConnection(strConnStr) Dim cmd As New SqlCommand Dim adataset As DataSet Dim adaptor As SqlDataAdapter cmd = New SqlCommand(cmdTxt, conn) cmd.CommandType = cmdType adaptor = New SqlDataAdapter(cmd) adataset = New DataSet Try If conn.State = ConnectionState.Closed Then conn.Open() End If adaptor.Fill(adataset) Catch ex As Exception MsgBox(ex.Message, , "数据库打开") Finally If conn.State = ConnectionState.Open Then conn.Close() End If End Try Return adataset.Tables(0) End Function End Class
在面向对象的学习中,我们知道当遇到两个以上的需求或用到两次以上的方法时我们就要考虑把它封装起来。SqlHelper类就是体现了封装的思想,它的提出是给了我们一个思路,其代码是根据具体情况具体分析的,切莫教条主义。
原文:http://blog.csdn.net/u010930340/article/details/39677079