vb.net它SqlHelper制备及应用

    上次文章中说到。对于一个项目来说。SqlHelper是一个非常重要的类。

    在正在构造的机房收费系统中。有大量的操作数据库的操作。

现在。把反复的代码所有拿出来,就形成了SqlHelper类。这个SqlHelper运行參数化查询。

至于使用。仅仅须要提供对应的sql语句和參数。

就能够运行数据库的操作了。

    实现声明一下啊。

这个样例 我也不知道能不能用在机房收费系统上。刚学完设计模式。突发奇想在数据库的链接上,加上了一个单例模式。假设出现什么不显示的情况。请联系我。

 

    接下来看一下。sqlHelper是怎样写的。以及怎样在D层中使用的。

 

Imports System.Data.SqlClient
Imports System.Configuration

Public NotInheritable Class SqlHelper
    ''' <summary>
    ''' 定义连接对象 和 cmd 命令
    ''' </summary>
    ''' <remarks></remarks>
    Dim ConnSql As SqlConnection    '连接字符串
    Dim cmdSql As New SqlCommand

    '创建链接,使用自己的链接方式(也算是一个单例模式吧)
    Public Sub New()
        ConnSql = SqlConnectionDAL.GetConn      '使用自己的sqlconnection
    End Sub

    ''' <summary>
    ''' 带參数的查询  
    ''' </summary>
    ''' <param name="strText">sql语句</param>
    ''' <param name="cmdType">查询类型</param>
    ''' <param name="sqlParams">句子中的參数</param>
    ''' <returns></returns>向数据库 提取 指定的数据
    ''' <remarks></remarks>
    Public Function Query(ByVal strText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable
        Dim sqlAdapater As SqlDataAdapter
        Dim dtSQL As New DataTable
        Dim dsSQL As New DataSet

        cmdSql.CommandText = strText
        cmdSql.CommandType = cmdType
        cmdSql.Connection = ConnSql

        cmdSql.Parameters.AddRange(sqlParams)
        sqlAdapater = New SqlDataAdapter(cmdSql)

        Try
            sqlAdapater.Fill(dsSQL)
            dtSQL = dsSQL.Tables(0)
            cmdSql.Parameters.Clear()
        Catch ex As Exception
            MsgBox(ex.Message, CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
        End Try

        Return dtSQL
    End Function

    ''' <summary>
    ''' 无參数的查询
    ''' </summary>
    ''' <param name="strText">sql语句</param>
    ''' <param name="cmdType">查询类型</param>
    ''' <returns></returns>主要是向数据库中取数据,用来使用
    ''' <remarks></remarks>
    Public Function QueryNo(ByVal strText As String, ByVal cmdType As CommandType) As DataTable
        Dim sqlAdapater As SqlDataAdapter
        Dim dtSQL As New DataTable
        Dim dsSQL As New DataSet

        cmdSql.CommandText = strText
        cmdSql.CommandType = cmdType
        cmdSql.Connection = ConnSql

        '加入參数
        sqlAdapater = New SqlDataAdapter(cmdSql)

        Try
            sqlAdapater.Fill(dsSQL)
            dtSQL = dsSQL.Tables(0)
            cmdSql.Parameters.Clear()
        Catch ex As Exception
            MsgBox(ex.Message, CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
        End Try

        Return dtSQL
    End Function

    ''' <summary>
    ''' 带參数的增删改
    ''' </summary>
    ''' <param name="strText">sql语句</param>
    ''' <param name="cmdType">查询类型:有參数的增删改</param>
    ''' <param name="sqlParams">參数</param>
    ''' <returns></returns>运行成功返回true
    ''' <remarks></remarks>
    Public Function UpdDelAlter(ByVal strText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As Boolean
        cmdSql.CommandText = strText
        cmdSql.CommandType = cmdType
        cmdSql.Connection = ConnSql

        '加入參数
        cmdSql.Parameters.AddRange(sqlParams)

        Dim flag As Boolean

        Try
            flag = cmdSql.ExecuteNonQuery
            cmdSql.Parameters.Clear()
            Return flag
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class

    在上面的样例中,还缺少那个 单例模式。假设感觉不合适的,请自行编写一个数据库的连接方式就能够了。

Imports System.Data.SqlClient
''' <summary>
''' 连接字符串 数据库  使用单例模式
''' </summary>
''' <remarks></remarks>赵崇 14-5-17
Public Class SqlConnectionDAL

    ''' <summary>
    ''' 定义一个连线字符串
    ''' </summary>
    ''' <remarks></remarks>赵崇 14-5-17
    Private Shared ReadOnly strLink As String = System.Configuration.ConfigurationManager.AppSettings("ConnStr")

    Public Shared conn As SqlConnection = Nothing

    '创建一个静态 仅仅读进程 辅助对象
    Public Shared ReadOnly syncRoot As New Object


    ''' <summary>
    ''' 私有构造函数,外部代码不能使用
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub New()
    End Sub

    Public Shared ReadOnly Property GetConn() As SqlConnection
        Get
            If conn Is Nothing Then
                SyncLock syncRoot       '锁,确保仅仅有一个链接可使用
                    If conn Is Nothing Then
                        conn = New SqlConnection(strLink)
                        conn.Open()
                    End If
                End SyncLock
            ElseIf conn.State = ConnectionState.Closed Then
                conn.Open()
            ElseIf conn.State = ConnectionState.Broken Then
                conn.Close()
                conn.Open()
            End If
            Return conn
        End Get
    End Property
End Class


     对于以上,项目的SqlHelper就编写完毕了。

仅仅须要使用就能够了。

至于使用。在D层的实现,这里仅仅写一个样例。

      咱们就写一个看看吧。

查询表:(不须要參数。如需使用 直接替换sql语句)

    ''' <summary>
    ''' 获取用户表中的用户级别信息
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetTable() As DataTable Implements IDAL.IUser.GetUserTable
        Dim strSQL As String = "Select distinct Level as [用户级别] from User_Info Des"

        Dim helper As New SqlHelper

        Dim dtUser = helper.QueryNo(strSQL, CommandType.Text)

        Return dtUser
    End Function

删除用户表:(更新 改动 等等 仅仅须要替换对应的SQl语句  和參数  就可以实现)

    ''' <summary>
    ''' 删除用户
    ''' </summary>
    ''' <param name="user">用户信息</param>
    ''' <returns></returns>返回true表示删除成功。返回false表示删除失败
    ''' <remarks></remarks>赵崇 14-5-21
    Public Function DeleteUser(ByVal user As UserEntity) As Boolean Implements IDAL.IUser.DeleteUser
        Dim strSQL As String
        strSQL = "Delete from User_Info where UserID=@UserID"
        Dim helper As New SqlHelper
        Dim sqlparameter As SqlParameter() = {New SqlParameter("@UserID", user.UserID)}

        Dim flag As Boolean
        flag = helper.UpdDelAlter(strSQL, CommandType.Text, sqlparameter)
        Return flag
    End Function


    通过上面的样例,你会发现,在于数据库进行操作的时候。只提供sql语句 加  參数。就能够达到自己的目的。

sqlHelper类,一次编写,到处使用。大量的节省代码。

    对于使用三层架构的程序。编写代码的时候,脑子里仅仅有 对象及其方法。在U层写代码的时候,不须要考虑B层的实现过程。仅仅知道B层的返回结果就能够了。

 


版权声明:本文博客原创文章。博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/mfrbuaa/p/4746818.html