本文主要是介绍机房收费系统——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 IntegerUsing 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层应经定义。Tryconn.Open() '打开连接Return cmd.ExecuteNonQuery '对数据库进行操作Catch ex As Exception '捕获异常Return 0Throw ex '抛出异常End TryEnd UsingEnd Function'对数据库进行增删改操作 有参数Public Function ExecuteNonQuery(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As IntegerUsing conn As New SqlConnection(strConnection) '定义连接Dim cmd As SqlCommand = conn.CreateCommand() '定义cmd命令cmd.CommandType = cmdTypecmd.CommandText = cmdTextcmd.Parameters.AddRange(sqlParams) '添加传进来的参数。Tryconn.Open()Return cmd.ExecuteNonQuery()Catch ex As ExceptionReturn 0Throw exEnd TryEnd UsingEnd Function' 查询操作 无参数Public Function ExecuteDataTable(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTableUsing 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对象Tryconn.Open() '打开连接adp.Fill(ds) '网dataset中填充数据Return ds.Tables(0) '返回dataset中第一个表Catch ex As ExceptionReturn NothingThrow exEnd TryEnd UsingEnd Function'执行查询操作 2 有参数Public Function ExecuteDataTable(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTableUsing conn As New SqlConnection(strConnection)Dim cmd As SqlCommand = conn.CreateCommand()Dim adp As SqlDataAdapterDim ds As New DataSetcmd.CommandText = cmdTextcmd.CommandType = cmdTypecmd.Parameters.AddRange(sqlParams)adp = New SqlDataAdapter(cmd)Tryconn.Open()adp.Fill(ds)Return ds.Tables(0)Catch ex As ExceptionReturn NothingThrow exEnd TryEnd UsingEnd Function
End Class
</span>
拿查询充值记录来说吧,下面是充值记录D层的代码。
Imports System.Data
Imports System.Data.SqlClient
Public Class QueryRechargeInfo1Public 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.TextDim helper As New SqlHelperDim 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总算有点了解了。
这篇关于机房收费系统——SqlHelper的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!