SQLHelper之简单方法封装

在做机房收费系统中SQLHelper类的封装是必不可少的。我们先来看看SQLHelper到底是个什么东西 ?


     SqlHelper是一个基于·NET Framework的数据库操作组件。组件中包含数据库操作方法。SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。SqlHelpe组件中包含数据库操作方法,SqlHelper有很多版本,主要以微软一开始发布的SqlHelper类,后面包含进了Enterprise Library开源包中了。还有一个主要版本是dbhelper.org开源的sqlhelper组件,优点是简洁,高性能,不仅仅支持sqlserver,同时支持sqlserver、oracle、access、Mysql数据库,也是一个开源项目,提供免费下载。


下面是 SQLHelper的一些简单方法封装。

代码:

Imports System.Data.SqlClient
''' <summary>
'''对数据库的查询,更新,删除数据操作
''' </summary>
''' <remarks>2013.12.20 by Gelupu</remarks>
Public Class SqlHelper

    Dim strCon As String
    Dim sqlCon As SqlConnection
    Dim sqlCmd As SqlCommand
    Dim dt As DataTable
    Dim sqlApt As SqlDataAdapter

    ''' <summary>
    ''' 构造方法,初始化sqlCon
    ''' </summary>
    ''' <remarks></remarks>
    Sub New()
        '设置连接字符串
        strCon = "Server=Gelupu-PC;Database=ChargeSys;User ID=sa;Password=123456"
        '初始化sqlCon
        sqlCon = New SqlConnection(strCon)
    End Sub

    ''' <summary>
    ''' 打开数据库连接
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub OpenConnection()
        sqlCon.Open()
    End Sub

    ''' <summary>
    ''' 关闭数据库连接
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub CloseConnection()
        sqlCon.Close()
        sqlCon = Nothing
    End Sub
    ''' <summary>
    ''' 执行不带参数的查询命令,返回数据表
    ''' </summary>
    ''' <param name="cmdText">SQL语句字符串</param>
    ''' <param name="cmdType">命令类型</param>
    ''' <returns>数据表</returns>
    ''' <remarks>2013.12.15 by Gelupu</remarks>

    Public Function ExecuteQueryNoParameters(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTable

        '通过sqlCon数据库连接对象的CreatCommand方法创建一SqlCommand实例对象
        sqlCmd = sqlCon.CreateCommand()
        '给SqlCommand对象CommandText属性赋值
        sqlCmd.CommandText = cmdText
        '给SqlCommand对象CommandType属性赋值
        sqlCmd.CommandType = cmdType
        '实例化DataTable
        dt = New DataTable
        '实例化SqlDataAdapter
        sqlApt = New SqlDataAdapter(sqlCmd)


        Try

            '打开数据库连接
            Me.OpenConnection()
            '执行命令,填充DataTable
            sqlApt.Fill(dt)
            '返回DataTable
            Return dt

        Catch ex As Exception
            '返回错误信息
            Return dt

        Finally
            '释放数据库操作对象
            If Not IsNothing(sqlCmd) Then
                sqlCmd.Dispose()
                sqlCmd = Nothing
            End If

            '关闭数据库连接
            If Not IsNothing(sqlCon) Then
                Me.CloseConnection()
            End If


        End Try


    End Function
    ''' <summary>
    ''' 执行带参数的查询命令,返回数据表
    ''' </summary>
    ''' <param name="cmdText">SQL查询语句字符串</param>
    ''' <param name="cmdType">命令类型</param>
    ''' <param name="Para">命令参数数组</param>
    ''' <returns>数据表</returns>
    ''' <remarks>2013/12/15 by Gelupu</remarks>
    ''' 
    Public Function ExecuteQueryWithParameters(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal para() As SqlParameter) As DataTable

        '通过sqlCon数据库连接对象的CreatCommand方法创建一SqlCommand实例对象
        sqlCmd = sqlCon.CreateCommand()
        '给SqlCommand对象CommandText属性赋值
        sqlCmd.CommandText = cmdText
        '给SqlCommand对象CommandType属性赋值
        sqlCmd.CommandType = cmdType
        '实例化DataTable
        dt = New DataTable
        '实例化SqlDataAdapter
        sqlApt = New SqlDataAdapter(sqlCmd)
        sqlCmd.Parameters.AddRange(para)

        Try

            '打开数据库连接
            Me.OpenConnection()
            '执行命令,填充DataTable
            sqlApt.Fill(dt)
            '返回DataTable
            Return dt

        Catch ex As Exception
            '返回错误信息
            Return dt

        Finally
            '释放数据库操作对象
            If Not IsNothing(sqlCmd) Then
                sqlCmd.Dispose()
                sqlCmd = Nothing
            End If

            '关闭数据库连接
            If Not IsNothing(sqlCon) Then
                Me.CloseConnection()
            End If


        End Try

    End Function
    ''' <summary>
    ''' 执行不带参数的增加,删除,修改命令,返回布尔值
    ''' </summary>
    ''' <param name="cmdText">SQL语句字符串</param>
    ''' <param name="cmdType">命令类型</param>
    ''' <returns>布尔值,标示命令是否执行成功</returns>
    ''' <remarks>2013.12.15 by Gelupu</remarks>
    Public Function ExecuteNotQueryNoParameters(ByVal cmdText As String, ByVal cmdType As CommandType) As Boolean

        '传入SQL语句和连接字符串,初始化SqlComm
        sqlCmd = New SqlCommand(cmdText, sqlCon)
        sqlCmd.CommandType = cmdType


        Try
            sqlCon.Open()
            sqlCmd.ExecuteNonQuery()
            Return True

        Catch ex As Exception
            Return False

        Finally
            '释放数据库操作对象资源
            If Not IsNothing(sqlCmd) Then
                sqlCmd.Dispose()
                sqlCmd = Nothing
            End If

            '关闭数据库连接
            If Not IsNothing(sqlCon) Then
                Me.CloseConnection()
            End If
        End Try


    End Function

    ''' <summary>
    ''' 执行带参数的数据库数据增加,删除,修改命令,返回布尔值
    ''' </summary>
    ''' <param name="cmdText">SQL语句字符串</param>
    ''' <param name="cmdType">命令类型</param>
    ''' <param name="para">命令参数数组</param>
    ''' <returns>布尔值,标示命令是否执行成功</returns>
    ''' <remarks>2013.12.15 by Gelupu</remarks>
    Public Function ExecuteNotQueryWithParameters(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal para() As SqlParameter) As Boolean

        '传入SQL语句和连接字符串,初始化SqlComm
        sqlCmd = New SqlCommand(cmdText, sqlCon)
        '设置命令类型
        sqlCmd.CommandType = cmdType
        '给命令参数赋值
        sqlCmd.Parameters.AddRange(para)

        Try
            '打开数据库连接
            sqlCon.Open()
            '执行命令,返回结果
            'Return  sqlCmd.ExecuteNonQuery()>0
            sqlCmd.ExecuteNonQuery()
            Return True

        Catch ex As Exception
            '返回失败
            Return False

        Finally
            '释放数据库操作对象资源
            If Not IsNothing(sqlCmd) Then
                sqlCmd.Dispose()
                sqlCmd = Nothing
            End If

            '关闭数据库连接
            If Not IsNothing(sqlCon) Then
                Me.CloseConnection()
            End If
        End Try


    End Function
    ''' <summary>
    ''' 设置参数命令
    ''' </summary>
    ''' <param name="paramName">命令参数名称</param>
    ''' <param name="sqlType">命令参数的数据类型</param>
    ''' <param name="strValue">命令参数值</param>
    ''' <returns>命令参数数组</returns>
    ''' <remarks>2013.12.15 by Gelupu</remarks>
    Public Function AddSqlParameter(ByVal paramName As String, ByVal sqlType As SqlDbType, ByVal strValue As String) As SqlParameter
        '初始化命令参数
        Dim sqlPara As New SqlParameter(paramName, sqlType)
        '设置命令参数的值
        sqlPara.Value = strValue

        '返回命令参数
        Return sqlPara

    End Function

End Class


你可能感兴趣的:(编程,.net,对象,database,sqlserver)