发布网友 发布时间:2022-04-07 19:25
共3个回答
懂视网 时间:2022-04-07 23:46
‘作者:刘文彬 ‘小组: ‘说明:利用函数重载,配置文件读取使得程序更加灵活 ‘创建日期:2015-4-27 ‘版本号:v1.4 ‘************************************************* Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Public Class SQLHelperDAL ‘全局配置:连接字符、定义一个cnn,cmd ‘增删改,有参数/无参数 ‘查,有参数/无参数 ‘全局配置 ‘连接字符串 ‘Dim ConnectionString As String = "Server=BillMYSQL;Database=Charge_System;User ID=sa;Password=" ‘初始化sqlconnection 对象 Dim cnn As New SqlConnection(Configuration.ConfigurationSettings.AppSettings("ConnectionString")) ‘读取配置文件,获得ConnectionString的值 Dim cmd As New SqlCommand #Region "有参增删改ExcuteNoQuery,不返回数据行,返回integer型数据表示受影响的行数。此函数被重载" ‘‘‘ <summary> ‘‘‘ 执行 增删改的数据库操作(有参数版) ‘‘‘ </summary> ‘‘‘ <param name="cmdText"></param>命令的文本 ‘‘‘ <param name="cmdType"></param>命令的类型 ‘‘‘ <param name="sqlParams"></param>要执行的sql语句集,无法确定具体条数 ‘‘‘ <returns></returns>整型值,表示受影响的行数 ‘‘‘ <remarks></remarks> Public Function ExcuteNoQuery(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As Integer ‘逐条将数组sqlParams中的参数添加到Parameters中 cmd.CommandType = cmdType ‘将命令的类型传给commandtype cmd.Connection = cnn ‘为执行连接命令的参数connection赋值 cmd.CommandText = cmdText ‘将sql语句赋值给执行命令的文本commandtext Try cmd.Parameters.Clear() ‘清除参数 ‘如果存在sqlparams参数,则添加到cmd对象中 If Not IsNothing(sqlParams) Then cmd.Parameters.AddRange(sqlParams) End If ‘如果没有打开连接,则打开连接 If cnn.State = ConnectionState.Closed Then cnn.Open() End If Return cmd.ExecuteNonQuery ‘执行操作 Catch ex As Exception Return 0 Finally ConnectionClose(cnn) CommandClose(cmd) End Try End Function #End Region #Region "无参增删改操作,不返回数据行,返回Integer类型数据表示受影响行数重载ExcuteNoQuery" ‘‘‘ <summary> ‘‘‘ 执行返回受影响的行数,此处是重载ExcuteNoQuery方法。 ‘‘‘ </summary> ‘‘‘ <param name="cmdText"></param>要执行的sql文本命令,或者存储过程名 ‘‘‘ <param name="cmdType"></param>要执行的命令的类型,sql文本命令或者存储过程名 ‘‘‘ <returns></returns>Integer类型的数,表示受影响的行数 ‘‘‘ <remarks></remarks> Public Function ExuteNoQuery(ByVal cmdText As String, ByVal cmdType As CommandType) As Integer Return ExcuteNoQuery(cmdText, cmdType, Nothing) End Function #End Region #Region "无参版,返回第一行第一列的值" Public Function ExcuteScalar(ByVal cmdText As String, cmdType As CommandType) As Object Return ExcuteScalar(cmdText, cmdType, Nothing) End Function #End Region #Region "获取第一行的第一列的值,有参版,该方法被重载" Public Function ExcuteScalar(ByVal cmdText As String, ByVal cmdType As CommandType, sqlParams As SqlParameter()) As Object Dim result As Object = Nothing cmd.Connection = cnn cmd.CommandType = cmdType cmd.CommandText = cmdText Try ‘如果状态没有打开则,进行连接打开操作 If cnn.State = ConnectionState.Closed Then cnn.Open() End If ‘如果非空则进行参数的全部添加 If Not IsNothing(sqlParams) Then cmd.Parameters.AddRange(sqlParams) End If result = cmd.ExecuteScalar Return result Catch ex As Exception Throw New Exception("ExcuteScalar错误:", ex) Finally ConnectionClose(cnn) CommandClose(cmd) End Try End Function #End Region #Region "查询操作,返回datetable,被重载的函数ExcuteSelect" ‘‘‘ <summary> ‘‘‘ 执行查询的操作,有参版 ‘‘‘ </summary> ‘‘‘ <param name="cmdText"></param>要执行的sql命令文本 ‘‘‘ <param name="cmdType"></param>命令类型 ‘‘‘ <param name="sqlParams"></param>要执行数据库操作的sql命令中的参数 ‘‘‘ <returns></returns>dataTable,查询得出的数据库中的数据表项 ‘‘‘ <remarks></remarks> Public Function ExcuteSelect(ByVal cmdText As String, ByVal cmdType As CommandType, sqlParams As SqlParameter()) As DataTable Dim sqlAdapter As SqlDataAdapter Dim dataTable As DataTable Dim dataSet As New DataSet cmd.Connection = cnn cmd.CommandType = cmdType cmd.CommandText = cmdText ‘为什么此处没有进行cnn.open连接操作也可以连接数据库???,难道是非连接状态的操作? sqlAdapter = New SqlDataAdapter(cmd) Try cmd.Parameters.Clear() ‘清除参数 ‘如果传来了sqlparams参数则,添加到cmd对象中 If Not IsNothing(sqlParams) Then cmd.Parameters.AddRange(sqlParams) End If sqlAdapter.Fill(dataSet) ‘用sqlAdapter 将dataset 填充 dataTable = dataSet.Tables(0) ‘dataTable 为dataSet 的第一个表 Return dataTable ‘返回获得到的数据 Catch ex As Exception Throw New Exception("查询失败!" + ex.Message) Finally ConnectionClose(cnn) CommandClose(cmd) End Try End Function #End Region #Region "无参查询,返回datetable,此处重载ExcuteSelect函数" ‘‘‘ <summary> ‘‘‘ 无参查询,返回datetable,此处重载ExcuteSelect ‘‘‘ </summary> ‘‘‘ <param name="cmdText"></param>要执行的sql文本命令或者存储过程名称 ‘‘‘ <param name="cmdType"></param>要执行的语句的类型,sql语句或者存储过程 ‘‘‘ <returns></returns>DataTable对象实例 ‘‘‘ <remarks></remarks> Public Function ExcuteSelect(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTable Return ExcuteSelect(cmdText, cmdType, Nothing) End Function #End Region ‘‘‘ <summary> ‘‘‘ 关闭连接对象,释放资源 ‘‘‘ </summary> ‘‘‘ <param name="cnn"></param> ‘‘‘ <remarks></remarks> Public Sub ConnectionClose(ByVal cnn As SqlConnection) If cnn.State = ConnectionState.Open Then cnn.Close() cnn = Nothing End If End Sub ‘‘‘ <summary> ‘‘‘ 关闭cmd对象,释放资源 ‘‘‘ </summary> ‘‘‘ <param name="cmd"></param> ‘‘‘ <remarks></remarks> Public Sub CommandClose(ByVal cmd As SqlCommand) If Not IsNothing(cmd) Then cmd.Dispose() cmd = Nothing End If End Sub End Class小结:最初自己就写了两个简单的函数,最后发现不够用了,只好自己继续进行重构SQLHelper类呗。发现自己第二次再写的时候,对于这个数据库访问助手类已经手到拈来了,写起代码来如行云流水,这大概就是学习的过程,重复次数多了,做多了也就会了。所以,没有你做不到的,请坚信自己一定可以的!
vb.net如何编写高效率的SQLHelper——(二)代码实战
标签:重载
热心网友 时间:2022-04-07 20:54
Dim num As Integer '次数热心网友 时间:2022-04-07 22:12
Dim strText As String = "fecvcsdwfchkeov[page]ove283673ewekl[page]fsdh5d37op"参考资料:http://blog.csdn.net/lmh27730536/article/details/3745530