首页 > 代码库 > 机房收费系统中sqlhelper的应用

机房收费系统中sqlhelper的应用

    在接受了三层的思想之后,sqlhelper被传的沸沸扬扬,它给我们的编码带来多少优势、让编码者少花了多少时间、多少精力,等等的赞美之词不绝于耳。自己也是将信将疑的,毕竟自己没有亲身经历,所以没有很大的体会。而如今,自己多次使用了sqlhelper,穿梭在各层之间确实也体会到了它带给我们的简便之处,结合很多人的版本自己编写了属于自己的那一版,以下便是具体代码:

<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration                            '在管理器中添加方可使用

'''<summary>
'''sqlhelper类
'''</summary>
''' <remarks ></remarks >
Public Class SQLHelperDAL
        '获得数据库的连接字串
        Private ReadOnly strconnection As String = "server=zhanghui-pc;database=charge-SYS;user id=sa;password=123456"

        '设置连接
        Dim conn As SqlConnection = New SqlConnection(strconnection)

        '定义cmd命令
        Dim cmd As New SqlCommand

        '''<summary>
        ''' 执行增、删、改三个操作(有参),
        '''</summary>
        ''' <param name="cmdtext">     </param >
        ''' <param name="cmdtype">     </param >
        ''' <param name="paras">       </param >
        ''' <returns></returns>        


    Public Function ExecAddDelUpdate(ByVal cmdtext As String, ByVal cmdtype As CommandType, ByVal paras As SqlParameter()) As Boolean
        '将传入的值分别赋给cmd
        cmd.Parameters.AddRange(paras)
        cmd.CommandType = cmdtype
        cmd.Connection = conn
        cmd.CommandText = cmdtext

        Try

            conn.Open()
            Return cmd.ExecuteNonQuery()
            cmd.Parameters.Clear()
        Catch ex As Exception
            Return 0
        Finally
            Call closecmd(cmd)
            Call closeconn(conn)
        End Try
    End Function

    '''<summary>
    ''' 执行增、删、改三个操作(无参)
    ''' </summary>
    ''' <param name="cmdtext">    </param >
    ''' <param name="cmdtype">    </param >
    ''' <returns>intergers        </returns >
    ''' <remarks >                </remarks >

    Public Function ExecAddDelUpdateNo(ByVal cmdtext As String, ByVal cmdtype As CommandType) As Integer
        '为要执行的cmd命令赋值
        cmd.CommandText = cmdtext
        cmd.CommandType = cmdtype
        cmd.Connection = conn

        '执行操作
        Try
            conn.Open()
            Return cmd.ExecuteNonQuery()
            cmd.Parameters.Clear()
        Catch ex As Exception
            Return 0
        Finally
            Call closecmd(cmd)
            Call closeconn(conn)
        End Try
    End Function

    '''<summary> 
    ''' 执行查询操作(有参)
    ''' </summary>
    ''' <param name="cmdtext"> </param>
    ''' <param name="cmdtype"> </param >
    ''' <param name="paras">   </param >
    ''' <returns></returns >
    ''' <remarks></remarks >
    Public Function ExecSelect(ByVal cmdtext As String, ByVal cmdtype As CommandType, ByVal paras As SqlParameter()) As DataTable
        Dim sqlAdapter As SqlDataAdapter
        Dim dt As New DataTable

        Dim ds As New DataSet


        '给cmd赋值
        cmd.CommandText = cmdtext
        cmd.CommandType = cmdtype
        cmd.Connection = conn
        cmd.Parameters.AddRange(paras)
        sqlAdapter = New SqlDataAdapter(cmd)                '实例化adapter对象
        Try
            sqlAdapter.Fill(ds)
            dt = ds.Tables(0)
            cmd.Parameters.Clear()

        Catch ex As Exception
            MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
        Finally
            Call closecmd(cmd)
        End Try

        Return dt
    End Function

    ''' <summary>
    ''' 执行查询的操作(无参)
    ''' </summary>
    ''' <param name="cmdtext"></param>
    ''' <param name="cmdtype"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ExecSelectNo(ByVal cmdtext As String, ByVal cmdtype As CommandType) As DataTable
        Dim sqlAdapter As SqlDataAdapter
        Dim ds As New DataSet

        '给cmd赋值
        cmd.CommandText = cmdtext
        cmd.CommandType = cmdtype
        cmd.Connection = conn
        sqlAdapter = New SqlDataAdapter(cmd)

        Try
            sqlAdapter.Fill(ds)
            Return ds.Tables(0)
        Catch ex As Exception
            Return Nothing

        Finally
            Call closecmd(cmd)

        End Try

    End Function

    ''' <summary>
    ''' 关闭连接
    ''' </summary>
    ''' <param name="conn"></param>
    ''' <remarks></remarks>
    Public Sub closeconn(ByVal conn As SqlConnection)
        If (conn.State <> ConnectionState.Closed) Then
            conn.Close()
            conn = Nothing
        End If
    End Sub

    ''' <summary>
    ''' 关闭cmd命令
    ''' </summary>
    ''' <param name="cmd"></param>
    ''' <remarks></remarks>
    Public Sub closecmd(ByVal cmd As SqlCommand)
        If Not IsNothing(cmd) Then
            cmd.Dispose()
            cmd = Nothing
        End If
    End Sub

End Class
</span>
    sqlhelper其实是对D层中连接字符串、创建command命令、执行sql操作等一系列固定动作的封装,它增加了代码的复用性、减轻了代码的书写压力,同时有利于后期的代码维护。sqlhelper更传递给我们一种思想,当有固定的、重复的事情要去做的话,我们可以把它定为一个模式,每次都这么去做,我们需要做的只是去处理那边细微的差别,这样既减轻了我们大脑的压力,同时又保证了事情的成功度,当然这种思想也在后边的泛型集合中得到了很好的体现!

    最后还是那句话,这只是鄙人的小小见解,有什么不妥的地方还请大家多多指正!

机房收费系统中sqlhelper的应用