最近在敲机房,对于SQLHelper也是心里有些怵,因为原来没用过,但是看了一些博客和资料后发现,如果不用这个类,会大大增加代码量,而且,其实它并不新,它是一个有多个关于数据库增删改查操作的语句函数的类,使用这个类可以方便D层调用,对数据库的操作,我们无非就是增删改查,而函数就是有无参数,有无返回值,SQLHelper是个新词,不过我们却一直在用,只是我们不这么叫,之所以叫SQLHelper,理所当然它是D层操作数据的助手,
下面用代码来展示一下这个类:
在用SQLHelper之前,我们要先用配置文件配置连接字符串,这样有利于在客户端更改数据库:
<appSettings > <add key ="Connstr" value ="datasource=.;database=ChargeSys;uid=sa;password=123456" /> </appSettings>这样就可以在SQLHelper中方便的使用连接字符串了,不管数据库怎么换,对源码都没有影响:
Imports System.Data Imports System.Configuration Imports System.Data.SqlClient Public Class SqlHelper '获得数据库连接字符串 Private ReadOnly strConn As String = ConfigurationManager.AppSettings("Connstr") '新建连接 Dim connSQL As SqlConnection = New SqlConnection(strConn) '定义命令 Dim cmdSQL As New SqlCommand ''' <summary> ''' 执行增删改,有参,确认是否执行成功 ''' </summary> ''' <param name="cmdSQLText">需要执行的数据库语句</param> ''' <param name="cmdSQLType">数据语句的类型,可能是sql类型,也可能是存储过程等</param> ''' <param name="sqlParams">参数数组,无法确定有多少参数</param> ''' <returns>返回受影响行数</returns> ''' <remarks></remarks> Public Function ExecuteAddDelUpdate(ByVal cmdSQLText As String, ByVal cmdSQLType As CommandType, ByVal sqlParams As SqlParameter()) As Integer cmdSQL.Parameters.AddRange(sqlParams) '将参数传入 cmdSQL.CommandType = cmdSQLType '确定语句类型,是sql还有oracle还是其他类型 cmdSQL.CommandText = cmdSQLText '将sql语句传给cmdSQL cmdSQL.Connection = connSQL '设置连接,全局变量 '开始执行查询 Try connSQL.Open() '打开连接 Return cmdSQL.ExecuteNonQuery() '执行查询 cmdSQL.Parameters.Clear() '清除参数 Catch ex As Exception Return 0 Finally Call CloseConnection(connSQL) Call CloseSQLCommand(cmdSQL) End Try End Function ''' <summary> ''' 执行增删改,有参,确认是否执行成功 ''' </summary> ''' <param name="cmdText">需要执行的数据库语句</param> ''' <param name="cmdType">数据语句的类型,一般为Sql语句,不是存储过程</param> ''' <returns>返回受影响的行数</returns> ''' <remarks></remarks> Public Function ExecuteAddDelUpdate(ByVal cmdText As String, ByVal cmdType As CommandType) As Integer cmdSQL.CommandType = cmdType '确定语句类型,是sql还有oracle还是其他类型 cmdSQL.CommandText = cmdText '将sql语句传给cmdSQL cmdSQL.Connection = connSQL '设置连接,全局变量 '开始执行查询 Try connSQL.Open() '打开连接 Return cmdSQL.ExecuteNonQuery() '执行查询 cmdSQL.Parameters.Clear() '清除参数 Catch ex As Exception Return 0 Finally Call CloseConnection(connSQL) Call CloseSQLCommand(cmdSQL) End Try End Function ''' <summary> ''' 执行查询操作,有参,返回datatable类型 ''' </summary> ''' <param name="cmdText">需要执行的数据库语句,一般SQL语句,也有存储过程</param> ''' <param name="cmdType">数据语句的类型,一般为Sql语句,不是存储过程</param> ''' <param name="sqlParams">参数数组,不确定有多少参数数组</param> ''' <returns>返回datatable类型</returns> ''' <remarks></remarks> Public Function ExecuteSelect(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable Dim sqlAdapter As SqlDataAdapter Dim dtSQL As New DataTable Dim dsSQL As New DataSet cmdSQL.CommandText = cmdText cmdSQL.CommandType = cmdType cmdSQL.Connection = connSQL cmdSQL.Parameters.AddRange(sqlParams) sqlAdapter = New SqlDataAdapter(cmdSQL) Try sqlAdapter.Fill(dsSQL) dtSQL = dsSQL.Tables(0) 'datatable为dataSet的第一个表 cmdSQL.Parameters.Clear() Catch ex As Exception MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告") Finally Call CloseSQLCommand(cmdSQL) End Try Return dtSQL End Function ''' <summary> ''' 执行查询操作,无参,返回datatable类型 ''' </summary> ''' <param name="cmdText">需要执行的数据库语句,一般SQL语句,也有存储过程</param> ''' <param name="cmdType">数据语句的类型,一般为Sql语句,不是存储过程</param> ''' <returns>返回datatable类型</returns> ''' <remarks></remarks> Public Function ExecuteSelect(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTable Dim sqlAdapter As SqlDataAdapter Dim dtSQL As New DataTable Dim dsSQL As New DataSet cmdSQL.CommandText = cmdText cmdSQL.CommandType = cmdType cmdSQL.Connection = connSQL sqlAdapter = New SqlDataAdapter(cmdSQL) '实例化adapter Try sqlAdapter.Fill(dsSQL) '用dsSQL填充sqlAdapter dtSQL = dsSQL.Tables(0) Catch ex As Exception Call CloseSQLCommand(cmdSQL) End Try Return dtSQL End Function ''' <summary> ''' 关闭连接 ''' </summary> ''' <param name="connSQL">需要关闭连接</param> ''' <remarks></remarks> Public Sub CloseConnection(ByVal connSQL As SqlConnection) If (connSQL.State <> ConnectionState.Closed) Then '如果没有关闭 connSQL.Close() '关闭连接 connSQL = Nothing '不指向原对象 End If End Sub ''' <summary> ''' 关闭命令 ''' </summary> ''' <param name="cmdSQL">需要关闭命令</param> ''' <remarks></remarks> Public Sub CloseSQLCommand(ByVal cmdSQL As SqlCommand) If Not IsNothing(cmdSQL) Then '如果存在命令 cmdSQL.Dispose() '将命令销毁 cmdSQL = Nothing End If End Sub End ClassSQLHelper将数据库操作语句分为了两类,一类是增删改、一类是查,这两类又分别有有参和无参两种,这些在D层有多个类需要操作数据库时,我们只需要调用这里的类,如果有参,只需要把参数穿进去就行了,下面看一看在D层我们是怎么用它的:
Imports System.Data Imports System.Data.SqlClient Imports DALHelper Imports IDAL Imports Entity Public Class LoginDAL : Implements IDAL.ILogin Public Function SelectUser(user As UserInfoEntity) As DataTable Implements ILogin.SelectUser Dim cmdText As String Dim sqlhelper As New SqlHelper Dim dtSQL As DataTable Dim sqlParams As SqlParameter() cmdText = "select * from T_UserInfo where UserName=@UserName and Password=@Password" sqlParams = {New SqlParameter("@UserName", user.UserName), New SqlParameter("@Password", user.Password)} dtSQL = sqlhelper.ExecuteSelect(cmdText, CommandType.Text, sqlParams) Return dtSQL End Function End Class这里执行的有参查询函数ExcuteSelect,对于SQLHelper的理解还有待提高,希望大家多多指正!