VB.NET版机房收费系统---SqlHelper
SqlHelper,最早接触这个词儿的时候,好像是13年的暑假,那个夏天来的比往年来的稍晚一些,呵呵,sqlhelper,翻译成中文就是数据库助手,帮手。百度百科这样对她进行阐述: SqlHelper是一个基于.NET Framework的数据库操作组件。组件中包含数据库操作方法。SqlHelper用于简化我们重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便! 可是,好好的,我们为什么要用SqlHlper?我想百度百科上的解释已经很清楚很明白了,SqlHlper是在D层中的代码抽象出来的,那D层中什么样的代码才能抽象出来?原来啊,就是把那些对数据库进行增删改查的操作,存储过程及程序集等中相同的代码抽象出来! 在 SqlHelper 类中实现的方法包括: 下面是SqlHelper的具体实现:
Imports System.Data.SqlClient Imports System.Configuration Public Class SqlHelper '定义连接字符串 Dim strConnection As String = System.Configuration.ConfigurationSettings.AppSettings("strConnection") '定义连接 Dim conn As SqlConnection '定义命令 Dim cmd As SqlCommand '初始化连接对象 Public Sub New() conn = New SqlConnection(strConnection) End Sub '/// <summary> '/// depiction:<有参数的非查询的操作> '/// </summary> '/// <param name="<strText>"><增删改语句或者存储过程></param> '/// <param name="<cmdType>"><命令类型文本或者存储过程></param> '/// <param name="<sqlParameter>"><参数数组></param> '/// <returns> '/// <返回布尔值> '/// </returns> Public Function ExecuteNonQuery(ByVal strText As String,ByVal cmdType As CommandType,ByVal sqlParameter As SqlParameter()) As Boolean Dim cmd As New SqlCommand '定义命令 cmd.CommandText = strText 'sql语句或存储过程名字 cmd.CommandType = cmdType '命令类型是StoredProcedure时,调用存储过程,一般为CommandText cmd.Connection = conn '连接数据库 cmd.Parameters.AddRange(sqlParameter) '传参 Dim flag As Boolean = False '定义返回值 Try conn.Open() '打开数据库连接 flag = cmd.ExecuteNonQuery cmd.Parameters.Clear() Catch ex As Exception flag = False Finally Call CloseConnection(conn) '关闭数据库连接 Call CloseCmd(cmd) End Try Return flag End Function '/// <summary> '/// depiction:<获取一个带参数的查询结果阅读器> '/// </summary> '/// <param name="<strText>"><增删改语句或者存储过程></param> '/// <param name="<cmdType>"><命令类型文本或者存储过程></param> '/// <param name="<sqlParameter>"><参数数组></param> '/// <returns> '/// <返回布尔值> '/// </returns> Public Function ExecuteReader(ByVal strText As String,ByVal sqlParameter As SqlParameter()) As Boolean Dim cmd As New SqlCommand Dim reader As SqlDataReader cmd.CommandText = strText 'sql语句或存储过程名字 cmd.CommandType = cmdType '命令类型是StoredProcdeure时,调用存储过程,一般为CommandText cmd.Connection = conn cmd.Parameters.AddRange(sqlParameter) '传参 Dim flag As Boolean = False '定义返回值 Try conn.Open() reader = cmd.ExecuteReader flag = reader.Read() cmd.Parameters.Clear() Catch ex As Exception flag = False Finally Call CloseConnection(conn) Call CloseCmd(cmd) End Try Return flag End Function '/// <summary> '/// depiction:<获取一个带参数的查询DataTable结果集> '/// </summary> '/// <param name="<strText>"><增删改语句或者存储过程></param> '/// <param name="<cmdType>"><命令类型文本或者存储过程></param> '/// <param name="<sqlParameter>"><参数数组></param> '/// <returns> '/// <返回DataTable> '/// </returns> Public Function ExecuteReaderTable(ByVal strText As String,ByVal sqlParameter As SqlParameter()) As DataTable Dim cmd As New SqlCommand '定义命令 Dim dataAdapter As New SqlDataAdapter '定义一个适配器对象 Dim dst As New DataSet Dim dt As New DataTable cmd.CommandText = strText 'sql语句或存储过程名字 cmd.CommandType = cmdType '命令类型是StoredProcdeure时,调用存储过程,一般为CommandText cmd.Connection = conn '连接数据库 cmd.Parameters.AddRange(sqlParameter) '传参 Try conn.Open() dataAdapter.SelectCommand = cmd dataAdapter.Fill(dst) dt = dst.Tables(0) Catch ex As Exception Call CloseConnection(conn) Call CloseCmd(cmd) End Try Return dt End Function '/// <summary> '/// depiction:<获取上机人数> '/// </summary> '/// <param name="<strText>"><增删改语句或者存储过程></param> '/// <param name="<cmdType>"><命令类型文本或者存储过程></param> '/// <param name="<sqlParameter>"><参数数组></param> '/// <returns> '/// <返回整型> '/// </returns> Public Function ExecuteScalar(ByVal cmdText As String,ByVal sqlParameter As SqlParameter()) As Integer Dim cmd As New SqlCommand Dim count As Integer cmd.CommandText = cmdText cmd.CommandType = cmdType cmd.Connection = conn cmd.Parameters.AddRange(sqlParameter) Try conn.Open() count = cmd.ExecuteScalar Catch ex As Exception Throw New Exception(ex.Message.ToString()) Finally Call CloseCounection(conn) Call CloseCmd(cmd) End Try Return count End Function '/// <summary> '/// depiction:<算取金额> '/// </summary> '/// <param name="<strText>"><增删改语句或者存储过程></param> '/// <param name="<cmdType>"><命令类型文本或者存储过程></param> '/// <param name="<sqlParameter>"><参数数组></param> '/// <returns> '/// <返回integer> '/// </returns> Public Function ExecuteScalarCash(ByVal cmdText As String,ByVal sqlParameter As SqlParameter()) As Decimal Dim cmd As New SqlCommand Dim cash As Decimal cmd.CommandText = cmdText cmd.CommandType = cmdType cmd.Connection = conn cmd.Parameters.AddRange(sqlParameter) Dim i As String Try conn.Open() i = cmd.ExecuteScalar.ToString() If i = "" Then cash = 0.0 Else cash = i End If Catch ex As Exception Throw New Exception(ex.Message.ToString()) Finally Call CloseConnection(conn) Call CloseCmd(cmd) End Try Return cash End Function Public Sub CloseConnection(ByVal conn As SqlConnection) If Not IsNothing(conn.State <> ConnectionState.Closed) Then conn.Close() '关闭连接 conn = Nothing End If End Sub Private Sub CloseCmd(cmd As SqlCommand) If Not IsNothing(cmd) Then '判断是否为空 cmd.Dispose() cmd = Nothing End If End Sub End ClassSqlHelper 封装成一个类,为开发人员选择访问数据库的方式提供了灵活性,每种方法的重载都支持不同的方法参数,因此开发人员可以确定传递连接、事务和参数信息的方式。像是打包,封装的思想完美诠释,抽离出相同的内容,使代码得到复用! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |