首页 > 代码库 > 机房收费系统个人重构关于SQLHelper

机房收费系统个人重构关于SQLHelper

              近期在敲机房,对于SQLHelper也是心里有些怵,由于原来没用过,可是看了一些博客和资料后发现,假设不用这个类,会大大添加代码量,并且,事实上它并不新,它是一个有多个关于数据库增删改查操作的语句函数的类,使用这个类能够方便D层调用,对数据库的操作,我们无非就是增删改查,而函数就是有无參数,有无返回值,SQLHelper是个新词,只是我们却一直在用,仅仅是我们不这么叫,之所以叫SQLHelper,理所当然它是D层操作数据的助手,

         在SQLHelper类中实现的方法包含:

                     ExecuteNonQuery。此方法用于运行不返回不论什么行或值的命令。这些命令通经常使用于运行数据库更新,但也可用于返回存储的输出參数。
           ExecuteReader。此方法用于返回对象,该对象包括由某一命令返回的结果集。
           ExecuteDataset。此方法返回DataSet对象,该对象包括由某一命令返回的结果集。
           ExecuteScalar。此方法返回一个值。该值始终是该命令返回的第一行的第一列。
        ExecuteXmlReader。此方法返回 FOR XML 查询的 XML 片段。
   在机房收费系统中,用到了一下方法:

         以下用代码来展示一下这个类:

         在用SQLHelper之前,我们要先用配置文件配置连接字符串,这样有利于在client更改数据库:

 <appSettings >
       <add key ="Connstr" value =http://www.mamicode.com/"datasource=.;database=ChargeSys;uid=sa;password=123456" />>         这样就能够在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 Class
          SQLHelper将数据库操作语句分为了两类,一类是增删改、一类是查,这两类又分别有有參和无參两种,这些在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的理解还有待提高,希望大家多多指正!