机房收费系统——SqlHelper

    前面自己将了一大堆ADO.NET的基础知识,现在该认真的实践一下了,下面我介绍一下vb.net版机房收费系统中,SqlHelper的使用,在机房收费中,SqlHelper是自己编写的一个类,里面用到的前面讲的几个对象的简单实例。

    SqlHelper简介:SqlHelper是一个基于·NETFramework的数据库操作组件。组件中包含数据库操作方法。SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。

    以上是百度百科中对SqlHelper的简介。在vb.net机房个人重构版本中,SqlHelper类还涉及到了函数的重载,函数重载的知识之前在C++中接触过,这里不做过多的描述。下面让我们来看一下SqlHelper这个类是怎样起到作用的吧!

vb.net版SqlHelper源码奉献

<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
 Public Class SqlHelper
    '调用配置文件
    Private ReadOnly strConnection As String = ConfigurationManager.AppSettings("Connstr").ToString
    '如果不调用配置文件,用字符串连接也行 下面所示
    ' Dim strConnection As String = "Data Source=自己的机器名;Initial Catalog=JiFang;Persist Security Info=True;User ID=sa;Password=55555"
    Public Function ExecuteNonQuery(ByRef cmdText As String, ByVal cmdType As CommandType) As Integer
        Using conn As New SqlConnection(strConnection)   '创建连接对象
            Dim cmd As SqlCommand = conn.CreateCommand() '创建命令对象
            cmd.CommandText = cmdText '获取要对数据源执行的文本命令,insert update  delete  
            cmd.CommandType = cmdType '将命令对象的CommandType属性值设定为cmdtype,这里是SQL文本命令。cmdType参数在D层应经定义。
            Try
                conn.Open()  '打开连接
                Return cmd.ExecuteNonQuery '对数据库进行操作
            Catch ex As Exception '捕获异常
                Return 0
                Throw ex   '抛出异常
            End Try
        End Using
    End Function
    '对数据库进行增删改操作  有参数
    Public Function ExecuteNonQuery(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As Integer
        Using conn As New SqlConnection(strConnection) '定义连接
            Dim cmd As SqlCommand = conn.CreateCommand() '定义cmd命令
            cmd.CommandType = cmdType
            cmd.CommandText = cmdText
            cmd.Parameters.AddRange(sqlParams) '添加传进来的参数。
            Try
                conn.Open()
                Return cmd.ExecuteNonQuery()
            Catch ex As Exception
                Return 0
                Throw ex
            End Try
        End Using
    End Function
    ' 查询操作 无参数
    Public Function ExecuteDataTable(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTable
        Using conn As New SqlConnection(strConnection)
            Dim cmd As SqlCommand = conn.CreateCommand()
            Dim adp As SqlDataAdapter  '声明一个 DataAdapter 对象
            Dim ds As New DataSet  '声明一个Dataset对象
            cmd.CommandText = cmdText  '同上
            cmd.CommandType = cmdType '同上
            adp = New SqlDataAdapter(cmd) '引用从数据源中检索的Command对象
            Try
                conn.Open()  '打开连接
                adp.Fill(ds)   '网dataset中填充数据
                Return ds.Tables(0) '返回dataset中第一个表
            Catch ex As Exception
                Return Nothing
                Throw ex
            End Try
        End Using
    End Function

    '执行查询操作 2 有参数
    Public Function ExecuteDataTable(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable
        Using conn As New SqlConnection(strConnection)
            Dim cmd As SqlCommand = conn.CreateCommand()
            Dim adp As SqlDataAdapter
            Dim ds As New DataSet
            cmd.CommandText = cmdText
            cmd.CommandType = cmdType
            cmd.Parameters.AddRange(sqlParams)
            adp = New SqlDataAdapter(cmd)
            Try
                conn.Open()
                adp.Fill(ds)
                Return ds.Tables(0)
            Catch ex As Exception
                Return Nothing
                Throw ex
            End Try
        End Using
    End Function
End Class
</span>
  拿查询充值记录来说吧,下面是充值记录D层的代码。

Imports System.Data
Imports System.Data.SqlClient
Public Class QueryRechargeInfo1
    Public Function QueryCharge(model As JFModel.QueryRechargeInfo) As IList(Of JFModel.QueryRechargeInfo) 
        Dim sqlparamas As SqlParameter() = {New SqlParameter("@CardNo", model.CardNO)}
        Dim cmdtxet As String = "select * from Recharge_Info where CardNO=@CardNo" '查询数据库中的信息
        Dim cmdtype As CommandType = New CommandType()
        cmdtype = CommandType.Text
        Dim helper As New SqlHelper
        Dim dt As New DataTable   '定义查询到的表集  
        Dim myList As List(Of JFModel.QueryRechargeInfo)    '保存转换后的泛型集合    
        dt = helper.ExecuteDataTable(cmdtxet, cmdtype, sqlparamas)   '执行查询  
        '将dt转换为泛型集合  
        myList = DataSetToList.converToList(Of JFModel.QueryRechargeInfo)(dt)
        Return myList    '返回一个实体  
    End Function
End Class
    以上便是我对SqlHelper的了解与应用,想想之前看见SqlHelper就害怕,里面的代码什么根本不知道为什么这么写,现在有了前面那几篇文章做铺垫,对SqlHelper,对ADO.NET总算有点了解了。

你可能感兴趣的:(sql,server,ADO.NET)