Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class SQLHelperDAL
Dim cnn
As New SqlConnection(Configuration.ConfigurationSettings.AppSettings(
"ConnectionString"))
Dim cmd
As New SqlCommand
#Region "有参增删改ExcuteNoQuery,不返回数据行,返回integer型数据表示受影响的行数。此函数被重载"
Public Function ExcuteNoQuery(
ByVal cmdText
As String,
ByVal cmdType
As CommandType,
ByVal sqlParams
As SqlParameter())
As Integer
cmd.CommandType = cmdType
cmd.Connection = cnn
cmd.CommandText = cmdText
Try
cmd.Parameters.Clear()
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"
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"
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
sqlAdapter =
New SqlDataAdapter(cmd)
Try
cmd.Parameters.Clear()
If Not IsNothing(sqlParams)
Then
cmd.Parameters.AddRange(sqlParams)
End If
sqlAdapter.Fill(dataSet)
dataTable = dataSet.Tables(
0)
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函数"
Public Function ExcuteSelect(
ByVal cmdText
As String,
ByVal cmdType
As CommandType)
As DataTable
Return ExcuteSelect(cmdText, cmdType,
Nothing)
End Function
#End Region
Public Sub ConnectionClose(
ByVal cnn
As SqlConnection)
If cnn.State = ConnectionState.Open
Then
cnn.Close()
cnn =
Nothing
End If
End Sub
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——(二)代码实战
标签:重载