sqlHelper的增删改查

       当一件事情被反复做了多次后。会想找一种办法来取代自己去做这个反复的动作。

敲代码也一样。

在程序中。对于反复的部分。假设是全然同样,那我们就会想着将其写成一个方法(过程、函数),放在一个具有权限的需求者都可以得着的地儿。

假设需求者在同一项目中。那么就把这种方法写成一个类。假设需求者在同一类中。那么就在本类中单独建一个方法写它。将同样的东西抽象出来。供多用户调用,就是用的抽象的思想。

       不论什么一个系统,都会涉及数据的传输、操作。而数据的操作概括起来不外乎增删改查(CURD),如今的系统随着使用者的增多,越来越多的用户操作,大数据频繁操作。

假设採用原来的方式来写。复杂的系统会造成大量赘余的代码。

    在类上方加入两条引用:

Imports System.Data.SqlClient       '引用SQL数据库连接
Imports System.Configuration        '引用配置文件

       建立一个操作数据库的SQLHelper类,

Public Class SQLHelper

    '获取配置文件里的连接字符串
    Private ReadOnly strSQLConnection As String = ConfigurationManager.AppSettings("sqlConcectStr")
    '定义连接
    Dim connSQL As SqlConnection = New SqlConnection(strSQLConnection)
    '定义cmd命令
    Dim cmdSQL As New SqlCommand
    '     ///<summary>
    '     ///depiction:<该方法是sqlhelper类的初始化>
    '     ///</summary>
    Public Sub New()
        connSQL = New SqlConnection(strSQLConnection)
    End Sub


    ''    ///<summary>
    ''    ///depiction:<该方法是关闭数据库的连接>
    ''    ///<summary>
    Private Sub CloseSQLConnection()
        '推断数据库连接对象状态是否为断开。假设还没有断,则断开
        If connSQL.State <> ConnectionState.Closed Then
            connSQL.Close()
        End If
    End Sub


    '     ///<summary>
    '     ///depiction:<该方法是关闭数据库命令>
    '     ///</summary>
    Private Sub CloseSQLCommand()
        '假设命令存在。则关闭
        If Not IsNothing(cmdSQL) Then
            cmdSQL.Dispose()   '销毁命令
            cmdSQL = Nothing
        End If
    End Sub


    '///<summary>
    ''//运行增删改三个操作。(有參)返回值为Boolean类型,确认是否运行成功
    '///</summary>
    '   ///<param name="strSql">须要运行语句。通常是Sql语句,也有存储过程</param>
    '   ///<param name="cmdType">推断Sql语句的类型,一般都不是存储过程</param>
    '   ///<returns>
    '   ///<返回Boolean,成功为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)   '參数传入
        cmdSQL.CommandType = cmdType            '
        cmdSQL.Connection = connSQL             '连接
        cmdSQL.CommandText = strSql             '查询语句

        Try
            connSQL.Open()                     '打开连接
            Return cmdSQL.ExecuteNonQuery()    '运行操作
            cmdSQL.Parameters.Clear()          '清除參数
        Catch ex As Exception
            Return False
        Finally
            Call CloseSQLConnection()          '关闭连接
            Call CloseSQLCommand()             '结束命令
        End Try

    End Function


    '///<summary>
    '///运行增删改三个操作,(无參)返回值为Boolean类型,确认是否运行成功
    ''///</summary>
    '///<param name="strSql">须要运行语句。通常是Sql语句,也有存储过程</param>
    '   ///<returns>
    '        ///<返回Boolean类型。成功为true,否则为false>
    '///</returns>
    Public Function ExecuteAddDelUpdate(ByVal strSql As String, ByVal cmdType As CommandType) As Boolean
        '用传进的參数填充类自己的cmd对象
        cmdSQL.CommandType = cmdType         '将
        cmdSQL.Connection = connSQL          '建立连接
        cmdSQL.CommandText = strSql          '设置查询语句


        Try
            connSQL.Open()                   '打开连接
            Return cmdSQL.ExecuteNonQuery()  '返回sql运行后受影响的行数
        Catch ex As Exception
            Return False
        Finally
            Call CloseSQLConnection()          '关闭连接
            Call CloseSQLCommand()             '结束命令
        End Try


    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, ByVal sqlParams As SqlParameter()) As DataTable
        Dim sqlAdapter As SqlDataAdapter
        Dim dtSQL As New DataTable
        Dim dsSQL As New DataSet

        '用传进的參数填充本类自己的cmd对象
        cmdSQL.Parameters.AddRange(sqlParams)      '传入參数
        cmdSQL.CommandType = cmdType
        cmdSQL.Connection = connSQL                '建立连接
        cmdSQL.CommandText = strSql                '查询语句

        sqlAdapter = New SqlDataAdapter(cmdSQL)    '实例化Adapter

        Try
            sqlAdapter.Fill(dsSQL)               '用Adater将DataSet填充
            dtSQL = dsSQL.Tables(0)              'DataTable为DataSet的第一个表
            cmdSQL.Parameters.Clear()            '清除參数
        Catch ex As Exception
            MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
        Finally
            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 = connSQL
        sqlAdapter = New SqlDataAdapter(cmdSQL)         '实例化Adapter

        Try
            sqlAdapter.Fill(dsSQL)                      '用Adaper将DataSet填充
            dtSQL = dsSQL.Tables(0)                     'DataTable为DataSet的第一个表
        Catch ex As Exception
            MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
        Finally
            Call CloseSQLCommand()
        End Try

        Return dtSQL

    End Function

End Class


原文地址:https://www.cnblogs.com/yxwkf/p/5097971.html