天天看点

SqlHelper类的使用    SqlHelper类      总结

    机房重构进行了一小阶段,刚开始敲代码的时候,实现每一个功能都要在D层类中写一次数据据库连接SqlConnection,SqlCommand,SqlDataReader语句,感到相当地麻烦。遇到这个问题后,就想到了面向对象的封装性,把D层中重复的SQL语句封装起来,以提高代码的复用性。通过上网查资料,知道了SqlHelper类和其使用方法。

    SqlHelper类将数据库连接(SqlConnection,SqlCommand,SqlDataReader)语句封装起来,在实现某一功能时,只需实力化SqlHelper类之后,给其中的方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。

    SqlHelper类    

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
'/************************************************* 
'类名:SqlHelper
'作者:孟浩杰
'说明:用户对数据库操作帮助类,对数据库的连接(sqlConnection,SqlCommand,SqlDataReader)等语句的封装。
'创建日期:2015-2-6
'版本号:机房收费系统1.0
'**********************************************/


Public Class SqlHelper
    '获取配置文件中的连接字符串
    Public ReadOnly strSQLConnection As String = System.Configuration.ConfigurationSettings.AppSettings("ConnStr")
    '定义连接
    Dim cnnSQL As New SqlConnection
    '定义Cmd命令
    Dim cmdSQL As New SqlCommand
    '///<summary>
    '///depiction<该方法是SqlHelper的初始化>
    '///<summary>
    Public Sub New()
        cnnSQL = New SqlConnection(strSQLConnection)
    End Sub
    '   /// <summary>  
    '   /// depiction:<改方法是打开数据库的连接>  
    '   /// </summary> 
    Public Sub openSQLConnection()
        '判断数据库连接对象状态是否为断开,如果断开就打开  
        If cnnSQL.State = ConnectionState.Closed Then
            cnnSQL.Open()
        End If
    End Sub
    '   /// <summary>  
    '   /// depiction:<改方法是关闭数据库的连接>  
    '   /// </summary>  
    Public Sub CloseSQLConnection()
        '判断数据库连接对象状态是否为断开,如果不断开就断开  
        If cnnSQL.State <> ConnectionState.Closed Then
            cnnSQL.Close()
        End If
    End Sub
    '/// <summary>  
    '/// depiction:<改方法是关闭数据库命令>  
    '/// </summary>  
    Public Sub CloseSQLCommand()
        '如果cmd命令存在  
        If Not IsNothing(cmdSQL) Then
            '销毁  
            cmdSQL.Dispose()
            cmdSQL = Nothing
        End If

    End Sub
    '/// <summary>    
    '///depiciton:<执行增删改三个操作,(有参)返回值为boolean类型,确认是否执行成功>
    '/// </summary>    
    '/// <param name="strSql">需要执行语句,一般是Sql语句,也有存储过程</param>    
    '///<param name="cmdType">判断Sql语句的类型,一般都不是存储过程</param>    
    '/// <param name="sqlParams">参数数组,无法确认有多少参数</param>  
    '/// <returns>  
    '    ///<返回布尔类型,成功为true,否则为false>  
    '/// </returns>  
    Public Function ExecuteAddDelUpdate(ByVal strSql As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As Boolean

        '用传进的参数填充本类自己的cmd对象  
        cmdSQL.Parameters.AddRange(sqlParams)
        '传入SQL语句的类型
        cmdSQL.CommandType = cmdType
        '设置连接 
        cmdSQL.Connection = cnnSQL
        '设置查询的语句 
        cmdSQL.CommandText = strSql

        Try
            '打开连接    
            cnnSQL.Open()
            '执行增删改操作 
            Return cmdSQL.ExecuteNonQuery()
            '清除参数
            cmdSQL.Parameters.Clear()
        Catch ex As Exception
            '如果出错,返回false 
            Return False
        Finally
            Call CloseSQLConnection()
            Call CloseSQLCommand()
        End Try

    End Function
    '/// <summary>    
    '///执行增删改三个操作,(无参)返回值为boolean类型,确认是否执行成功    
    '/// </summary>    
    '/// <param name="strSql">需要执行语句,一般是Sql语句,也有存储过程</param>    
    '///<param name="cmdType">判断Sql语句的类型,一般都不是存储过程</param>    
    '/// <returns>  
    '///<返回布尔类型,成功为true,否则为false>  
    '/// </returns>  
    Public Function ExecuteAddDelUpdate(ByVal strSql As String, ByVal cmdType As CommandType) As Boolean

        '用传进的参数填充本类自己的cmd对象  
        cmdSQL.CommandType = cmdType
        '将参数传入 
        cmdSQL.Connection = cnnSQL
        '设置查询的语句
        cmdSQL.CommandText = strSql

        Try
            '打开连接 
            cnnSQL.Open()
            '执行增删改操作    
            Return cmdSQL.ExecuteNonQuery()
        Catch ex As Exception
            '如果出错,返回false 
            Return False
        Finally
            Call CloseSQLConnection()
            Call CloseSQLCommand()
        End Try

    End Function

    '/// <summary>    
    ''///执行查询操作,(有参)返回值为datatable类型    
    '/// </summary>    
    '/// <param name="strSql">需要执行语句,一般是Sql语句,也有存储过程</param>    
    '///<param name="cmdType">判断Sql语句的类型,一般都不是存储过程</param>    
    '/// <param name="sqlParams">参数数组,无法确认有多少参数</param>  
    '/// <returns>  
    '///<返回表>  
    '/// </returns>  
    Public Function ExecuteSelect(ByVal strSql 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

        '用传进的参数填充本类自己的cmd对象    
        cmdSQL.CommandText = strSql
        cmdSQL.CommandType = cmdType
        cmdSQL.Connection = cnnSQL
        '参数添加
        cmdSQL.Parameters.AddRange(sqlParams)
        '实例化adapter      
        sqlAdapter = New SqlDataAdapter(cmdSQL)

        Try
            '用adapter将dataSet填充 
            sqlAdapter.Fill(dsSQL)
            'datatable为dataSet的第一个表 
            dtSQL = dsSQL.Tables(0)
            '清除参数   
            cmdSQL.Parameters.Clear()
        Catch ex As Exception
            MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
        Finally
            '最后一定要销毁cmd  
            Call CloseSQLCommand()
        End Try

        Return dtSQL

    End Function

    '/// <summary>    
    ''///执行查询操作,(无参)返回值为datatable类型    
    '/// </summary>    
    '/// <param name="strSql">需要执行语句,一般是Sql语句,也有存储过程</param>    
    '///<param name="cmdType">判断Sql语句的类型,一般都不是存储过程</param>    
    '/// <returns>  
    '///<返回表>  
    '/// </returns> 
    Public Function ExecuteSelect(ByVal strSql As String, ByVal cmdType As CommandType) As DataTable
        Dim sqlAdapter As SqlDataAdapter
        Dim dtSQL As New DataTable
        Dim dsSQL As New DataSet

        '用传进的参数填充本类自己的cmd对象    
        cmdSQL.CommandText = strSql
        cmdSQL.CommandType = cmdType
        cmdSQL.Connection = cnnSQL
        '实例化adapter
        sqlAdapter = New SqlDataAdapter(cmdSQL)
        Try
            '用adapter将dataSet填充
            sqlAdapter.Fill(dsSQL)
            'datatable为dataSet的第一个表
            dtSQL = dsSQL.Tables(0)
        Catch ex As Exception
            MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
        Finally
            '最后一定要销毁cmd   
            Call CloseSQLCommand()
        End Try

        Return dtSQL

    End Function


End Class
           

  总结

  在机房重构的过程中,我们会遇到写很多重复的代码的情况。遇到这种情况,我们就要多去想想去怎样把这些重复的代码封装起来,封装起来的好处。对我们来说这是一种成长。

继续阅读