在上一篇博客中,我们提到了SqlHelper,那么什么是SqlHelper呢?这篇博客我们就来介绍一下。
记得第一次听SqlHelper这个词,还不知道是干什么用的。现在终于可以揭开它神秘的面纱了。
一开始我们敲机房的时候大概都有所体会,有很多与数据库连接有关的代码需要我们一直重复。而SqlHelper就是用于简化那些重复的代码的。
来自百度百科比较“权威”的解释:SqlHelper是一个基于·NET Framework的数据库操作组件。组件中包含数据库操作方法。SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。
我们再来看一下SqlHelper的成员:
上一篇博客对于登录的介绍并没有涉及配置文件的内容。这里就提一下。
我们在应用SqlHelper之前,可以在配置文件中设置数据库连接的字符串,这样增加了程序的可移植性,也大大简化了代码。
<appSettings> <add key="strConnection" value ="Data Source=localhost;Initial Catalog=Refactor;User ID=sa;Password=199312"></add> <add key="strDB" value ="Sql"></add> </appSettings>看一下SqlHelper的代码:
Imports System.Data.SqlClient Imports System.Configuration Public Class SqlHelper '定义连接字符串 Dim strConnection As String = System.Configuration.ConfigurationSettings.AppSettings("strConnection") '定义连接 Dim conn As SqlConnection '初始化连接对象 Dim cmd As SqlCommand Public Sub New() conn = New SqlConnection(strConnection) End Sub '有参数的非查询的操作,执行增删改操作,返回Boolean,确认是否执行成功
Public Function ExecuteNonQuery(ByVal strText As String, ByVal cmdType As CommandType, ByVal sqlParameter As SqlParameter()) As Boolean '定义命令 Dim cmd As New SqlCommand 'sql语句或存储过程名字 cmd.CommandText = strText '命令类型是StoredProcedure时,调用存储过程,一般为CommandText cmd.CommandType = cmdType '连接数据库 cmd.Connection = conn '传参 cmd.Parameters.AddRange(sqlParameter) Dim flag As Boolean = False Try conn.Open() ‘打开连接 flag = cmd.ExecuteNonQuery ’执行增删改操作 cmd.Parameters.Clear() ’清楚参数 Catch ex As Exception flag = False '出错就返回False Finally Call CloseConnection(conn) Call CloseCmd(cmd) End Try Return flag End Function ’获取一个带参数的查询结果阅读器 Public Function ExecuteReader(ByVal strText As String, ByVal cmdType As CommandType, ByVal sqlParameter As SqlParameter()) As Boolean Dim cmd As New SqlCommand Dim reader As SqlDataReader cmd.CommandText = strText cmd.CommandType = cmdType cmd.Connection = conn cmd.Parameters.AddRange(sqlParameter) Dim flag As Boolean = False Try conn.Open() reader = cmd.ExecuteReader flag = reader.Read() cmd.Parameters.Clear() Catch ex As Exception flag = False Finally Call CloseConnection(conn) Call CloseCmd(cmd) End Try Return flag End Function '获取一个带参数的查询DataTable结果集 Public Function ExecuteReaderTable(ByVal strText As String, ByVal cmdType As CommandType, ByVal sqlParameter As SqlParameter()) As DataTable '定义命令 Dim cmd As New SqlCommand '定义一个适配器对象 Dim dataAdapter As New SqlDataAdapter Dim dst As New DataSet Dim dt As New DataTable cmd.CommandText = strText cmd.CommandType = cmdType cmd.Connection = conn cmd.Parameters.AddRange(sqlParameter) Try conn.Open() dataAdapter.SelectCommand = cmd dataAdapter.Fill(dst) dt = dst.Tables(0) Catch ex As Exception Call CloseConnection(conn) Call CloseCmd(cmd) End Try Return dt End Function ’返回一个值,该值始终是该命令返回的第一行第一列 Public Function ExecuteScalar(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParameter As SqlParameter()) As Integer Dim cmd As New SqlCommand Dim count As Integer cmd.CommandText = cmdText cmd.CommandType = cmdType cmd.Connection = conn cmd.Parameters.AddRange(sqlParameter) Try conn.Open() count = cmd.ExecuteScalar Catch ex As Exception Throw New Exception(ex.Message.ToString()) Finally Call CloseConnection(conn) Call CloseCmd(cmd) End Try Return count End Function Public Sub CloseConnection(ByVal conn As SqlConnection) If Not IsNothing(conn.State <> ConnectionState.Closed) Then ’如果没有关闭 conn.Close() conn = Nothing ’关闭连接,不指向原对象 End If End Sub Private Sub CloseCmd(cmd As SqlCommand) If Not IsNothing(cmd) Then ‘如果cmd存在,则销毁 cmd.Dispose() cmd = Nothing End If End Sub End Class对于SqlHelper的理解还不是很深,还要多多加强!