刚刚开始时一直纠结这个Sqlhelper,听起来感觉有点了不起。真正用起来才知道它不过就是对D层中相同代码的封装。由于我是先用纯三层敲了一遍机房,敲第二遍的时候加设计模式和Sqlhelper,感觉真是水到渠成。将变化的地方作成参数,传入到SqlHelper的函数中。当然这个过程中,需要分析我们写在D层中函数的特点。所幸已经有人帮我们总结了出来。我们学会站在巨人的肩膀上。
'/************************************************* '类名称:SqlHelper '命名空间:Helper '作者:王雅瑾 '小组: '说明: '创建日期:2013/11/16 17:13:13 '版本号:v2.0.0 '**********************************************/ Imports System.Data Imports System.Data.SqlClient Public Class SqlHelper Private ReadOnly strConnection As String = Configuration.ConfigurationManager.ConnectionStrings("ChargeSystemConnectionString").ToString '调用配置文件 ''' <summary> ''' ExecuteNonQuery操作,对数据库进行增删改操作(1)无参数 ''' </summary> ''' <param name="cmdText">需要执行的Sql语句</param> ''' <param name="cmdType">给出Sql语句的类型</param> ''' <returns>integer,受影响的行数</returns> ''' <remarks></remarks> Public Function ExecuteNonQuery(ByVal cmdText As String, ByVal cmdType As CommandType) As Integer Using conn As New SqlConnection(strConnection) '使用连接池,可以在使用完成后自动关闭连接. Dim cmd As SqlCommand = conn.CreateCommand() cmd.CommandText = cmdText cmd.CommandType = cmdType Try conn.Open() Return cmd.ExecuteNonQuery() Catch ex As Exception Return 0 Throw ex End Try End Using End Function
''' <summary> ''' ExecuteNonQuery操作,对数据库进行增删改操作(2)有参数 ''' </summary> ''' <param name="cmdText">需要执行的Sql语句</param> ''' <param name="cmdType">给出Sql语句的类型</param> ''' <param name="sqlParams">参数数组,参数个数根据实际情况而定</param> ''' <returns>integer,受影响的行数</returns> ''' <remarks></remarks> Public Function ExecuteNonQuery(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As Integer Using conn As New SqlConnection(strConnection) Dim cmd As SqlCommand = conn.CreateCommand() cmd.CommandText = cmdText cmd.CommandType = cmdType cmd.Parameters.AddRange(sqlParams) Try conn.Open() Return cmd.ExecuteNonQuery() Catch ex As Exception Return 0 Throw ex End Try End Using End Function
''' <summary> ''' 执行查询操作(1)无参数 ''' </summary> ''' <param name="cmdText">需要查询的Sql语句</param> ''' <param name="cmdType">给出Sql语句的类型</param> ''' <returns>dataTable,查询到的表格</returns> ''' <remarks></remarks> Public Function ExecuteDataTable(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTable Using conn As New SqlConnection(strConnection) Dim cmd As SqlCommand = conn.CreateCommand() Dim adp As SqlDataAdapter Dim ds As New DataSet cmd.CommandText = cmdText cmd.CommandType = cmdType adp = New SqlDataAdapter(cmd) Try conn.Open() adp.Fill(ds) Return ds.Tables(0) Catch ex As Exception Return Nothing Throw ex End Try End Using End Function
''' <summary> ''' 执行查询操作(2)有参数 ''' </summary> ''' <param name="cmdText">需要执行的Sql语句</param> ''' <param name="cmdType">给出Sql语句的类型</param> ''' <param name="sqlParams">参数数组,参数个数根据实际情况而定</param> ''' <returns>dataTable,查询出的表格</returns> ''' <remarks></remarks> Public Function ExecuteDataTable(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable Using conn As New SqlConnection(strConnection) Dim cmd As SqlCommand = conn.CreateCommand() Dim adp As SqlDataAdapter Dim ds As New DataSet cmd.CommandText = cmdText cmd.CommandType = cmdType cmd.Parameters.AddRange(sqlParams) adp = New SqlDataAdapter(cmd) Try conn.Open() adp.Fill(ds) Return ds.Tables(0) Catch ex As Exception Return Nothing Throw ex End Try End Using End Function End Class
''' <summary> ''' 登录时查询用户是否存在. ''' </summary> ''' <param name="enUser">参数为用户实体</param> ''' <returns>datatable ,将userPassword,level,作为表中的两列返回</returns> ''' <remarks></remarks> Public Function SelectUser(enUser As Entity.UsersEntity) As DataTable Implements IDAL.IUser.SelectUser '对应于Sqlhelper中方法4 Dim helper As New Helper.SqlHelper Dim dt As New DataTable Dim cmdText As String = "select userPassword,level from [T-Users] where userId=@userId and isOn='是'" Dim sqlParams As SqlParameter() = {New SqlParameter("@userId", enUser.UserId)} dt = helper.ExecuteDataTable(cmdText, CommandType.Text, sqlParams) Return dt End Function