对于数据处理逻辑,上篇已经发布了又VB.net写的接口,这里公布一个又vb.net写的接口实现类
本来想发布类工厂操作,但是我的工程采用了配置文件注入方式,所以代码又点长,就先发sql数据库操作的实现类。
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
''' <summary>
''' 简单数据操作底层类SQL数据库
''' </summary>
''' <remarks>
''' 该类执行简单的数据操作支持SQL
''' 开发者:欧阳寒玟
''' 开发时间:2009-01-25
''' 修改时间:2010-10-25
''' 作者网站:http://www.coldwin.org
''' </remarks>
Public Class SQLHelper
Implements IDbHelper
Private conStr As String
Private conn As SqlConnection ''连接对象
Private cmd As SqlCommand ''Command对象
Private _isTran As Boolean = False ''是否执行事务
#Region "准备工作"
''' <summary>
''' 获取或设置链接字符串
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ConnectionString As String Implements IDbHelper.ConnectionString
Get
Return conStr
End Get
Set(ByVal value As String)
conStr = value
End Set
End Property
''' <summary>
''' 获取或设置连接对象
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Connection As IDbConnection Implements IDbHelper.Connection
Get
Return conn
End Get
Set(ByVal value As IDbConnection)
conn = value
End Set
End Property
''' <summary>
''' 是否执行事务
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property isTranSaction As Boolean Implements IDbHelper.isTranSaction
Get
Return _isTran
End Get
Set(ByVal value As Boolean)
_isTran = value
End Set
End Property
''' <summary>
''' 获取或设置数据执行事务
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property TranSaction As IDbTransaction Implements IDbHelper.TranSaction
Get
'isTranSaction = True
Return cmd.Transaction
End Get
Set(ByVal value As IDbTransaction)
isTranSaction = True
cmd.Transaction = value
End Set
End Property
''' <summary>
''' 关闭Connection对象
''' </summary>
''' <remarks></remarks>
Public Sub Close() Implements IDbHelper.Close
If conn.State = ConnectionState.Open And isTranSaction = False Then
conn.Close()
End If
End Sub
''' <summary>
''' 默认读取配置文件的链接字符串
''' </summary>
''' <remarks></remarks>
Public Sub New()
Try
conStr = Configuration.ConfigurationManager.AppSettings("sql").ToString()
Catch ex As Exception
Try
conStr = System.Configuration.ConfigurationManager.ConnectionStrings("sql").ToString()
Catch exp As Exception
CWF.DocumentService.ServerSession(Of String).log.Save(exp.Message)
End Try
End Try
conn = New SqlConnection(ConnectionString)
cmd = New SqlCommand()
cmd.Connection = conn
Try
conn.Open()
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
''' <summary>
''' 带参数实例化
''' </summary>
''' <param name="constring">链接字符串</param>
''' <remarks></remarks>
Public Sub New(ByVal constring As String)
ConnectionString = constring
conn = New SqlConnection(ConnectionString)
cmd = New SqlCommand()
cmd.Connection = conn
Try
conn.Open()
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
#End Region
Private Function Exec(ByVal Command As SqlCommand, ByVal CmdTxt As String, ByVal commondType As CommandType) As Integer
Command.CommandText = CmdTxt
Command.CommandType = commondType
Try
If conn.State = ConnectionState.Closed Then conn.Open()
Return Command.ExecuteNonQuery()
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
Close()
End Try
End Function
Private Function Exec(ByVal Command As SqlCommand, ByVal CmdTxt As String, ByVal commondType As CommandType, ByVal Parameters() As SqlParameter) As Integer
Command.CommandText = CmdTxt
Command.CommandType = commondType
Command.Parameters.AddRange(CType(Parameters, SqlParameter()))
Try
If conn.State = ConnectionState.Closed Then conn.Open()
Return Command.ExecuteNonQuery()
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
Close()
End Try
End Function
Private Function qureys(ByVal Command As SqlCommand, ByVal CmdTxt As String, ByVal commandType As CommandType) As DataTable
Dim dt As New DataTable
Command.CommandText = CmdTxt
Command.CommandType = commandType
Dim DataAdapter As New SqlDataAdapter(Command)
If conn.State = ConnectionState.Closed Then conn.Open()
Try
DataAdapter.Fill(dt)
Return dt
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
Close()
End Try
End Function
Private Function qureys(ByVal Command As SqlCommand, ByVal CmdTxt As String, ByVal commandType As CommandType, ByVal Parameters() As SqlParameter) As DataTable
Dim dt As New DataTable
Command.CommandText = CmdTxt
Command.CommandType = commandType
Command.Parameters.AddRange(CType(Parameters, SqlParameter()))
Dim DataAdapter As New SqlDataAdapter(Command)
If conn.State = ConnectionState.Closed Then conn.Open()
Try
DataAdapter.Fill(dt)
Return dt
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
Close()
End Try
End Function
Private Function qureysDataset(ByVal Command As SqlCommand, ByVal CmdTxt As String, ByVal commandType As CommandType) As DataSet
Dim dt As New DataSet
Command.CommandText = CmdTxt
Command.CommandType = commandType
Dim DataAdapter As New SqlDataAdapter(Command)
If conn.State = ConnectionState.Closed Then conn.Open()
Try
DataAdapter.Fill(dt)
Return dt
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
Close()
End Try
End Function
Private Function qureysDataset(ByVal Command As SqlCommand, ByVal CmdTxt As String, ByVal commandType As CommandType, ByVal Parameters() As SqlParameter) As DataSet
Dim dt As New DataSet
Command.CommandText = CmdTxt
Command.CommandType = commandType
Command.Parameters.AddRange(CType(Parameters, SqlParameter()))
Dim DataAdapter As New SqlDataAdapter(Command)
If conn.State = ConnectionState.Closed Then conn.Open()
Try
DataAdapter.Fill(dt)
Return dt
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
Close()
End Try
End Function
''' <summary>
''' 执行无参数存储过程
''' </summary>
''' <param name="ProcName">存储过程名称</param>
''' <remarks></remarks>
Public Sub ExcuteProc(ByVal ProcName As String) Implements IDbHelper.ExcuteProc
Exec(cmd, ProcName, CommandType.StoredProcedure)
End Sub
''' <summary>
''' 执行带参数的存储过程
''' </summary>
''' <param name="ProcName">存储过程名称</param>
''' <param name="Parameters">参数数组</param>
''' <remarks></remarks>
Public Sub ExcuteProc(ByVal ProcName As String, ByVal Parameters() As IDbDataParameter) Implements IDbHelper.ExcuteProc
Exec(cmd, ProcName, CommandType.StoredProcedure, CType(Parameters, SqlParameter()))
End Sub
''' <summary>
''' 执行无参数sql语句
''' </summary>
''' <param name="cmdTxt">执行语句</param>
''' <returns>影响行数</returns>
''' <remarks></remarks>
Public Function Execute(ByVal cmdTxt As String) As Integer Implements IDbHelper.Execute
Return Exec(cmd, cmdTxt, CommandType.Text)
End Function
''' <summary>
''' 执行带参数的sql语句
''' </summary>
''' <param name="cmdTxt">命令语句</param>
''' <param name="Parameters">参数</param>
''' <returns>影响行数</returns>
''' <remarks></remarks>
Public Function Execute(ByVal cmdTxt As String, ByVal Parameters() As IDbDataParameter) As Integer Implements IDbHelper.Execute
Return Exec(cmd, cmdTxt, CommandType.Text, Parameters)
End Function
''' <summary>
''' 启用内部事务执行多条命令
''' </summary>
''' <param name="cmdTxt">命令数组</param>
''' <param name="cmdType">执行类型</param>
''' <returns>影响行数</returns>
''' <remarks></remarks>
Public Function ExecuteNonQuery(ByVal cmdTxt() As String, ByVal cmdType As System.Data.CommandType) As Integer Implements IDbHelper.ExecuteNonQuery
cmd.CommandType = cmdType
Dim tran As SqlTransaction = Nothing
Dim num As Integer = 0
Try
tran = conn.BeginTransaction
For Each st As String In cmdTxt
If st = "" Then Exit For
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
cmd.CommandText = st
cmd.Transaction = tran
num += cmd.ExecuteNonQuery()
Next
tran.Commit()
Catch ex As Exception
tran.Rollback()
num = 0
Throw New Exception(ex.Message)
Finally
cmd.Parameters.Clear()
Close()
End Try
Return num
End Function
''' <summary>
''' 执行无参数sql命令返回第一行第一列
''' </summary>
''' <param name="cmdTxt">sql命令</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ExecuteScalar(ByVal cmdTxt As String) As Object Implements IDbHelper.ExecuteScalar
cmd.CommandText = cmdTxt
If conn.State = ConnectionState.Closed Then conn.Open()
Try
Return cmd.ExecuteScalar()
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
Close()
End Try
End Function
''' <summary>
''' 执行带参数sql命令返回第一行第一列
''' </summary>
''' <param name="cmdTxt">sql命令</param>
''' <param name="Parameters" >参数</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ExecuteScalar(ByVal cmdTxt As String, ByVal Parameters() As IDbDataParameter) As Object Implements IDbHelper.ExecuteScalar
cmd.CommandText = cmdTxt
cmd.Parameters.AddRange(CType(Parameters, SqlParameter()))
If conn.State = ConnectionState.Closed Then conn.Open()
Try
Return cmd.ExecuteScalar()
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
Close()
End Try
End Function
''' <summary>
''' 执行无参数命令返回DataTable
''' </summary>
''' <param name="cmdTxt">SQL命令</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Query(ByVal cmdTxt As String) As System.Data.DataTable Implements IDbHelper.Query
Return Me.qureys(cmd, cmdTxt, CommandType.Text)
End Function
''' <summary>
''' 执行带参数命令返回DataTable
''' </summary>
''' <param name="cmdTxt">SQL</param>
''' <param name="Parameters">参数</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Query(ByVal cmdTxt As String, ByVal Parameters() As IDbDataParameter) As System.Data.DataTable Implements IDbHelper.Query
Return Me.qureys(cmd, cmdTxt, CommandType.Text, Parameters)
End Function
''' <summary>
''' 执行无参数命令返回DataSet
''' </summary>
''' <param name="cmdTxt">SQL命令</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function QueryDataSet(ByVal cmdTxt As String) As System.Data.DataSet Implements IDbHelper.QueryDataSet
Return Me.qureysDataset(cmd, cmdTxt, CommandType.Text)
End Function
''' <summary>
''' 执行带参数命令返回DataSet
''' </summary>
''' <param name="cmdTxt">SQL</param>
''' <param name="Parameters">参数</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function QueryDataSet(ByVal cmdTxt As String, ByVal Parameters() As Object) As System.Data.DataSet Implements IDbHelper.QueryDataSet
Return Me.qureysDataset(cmd, cmdTxt, CommandType.Text, Parameters)
End Function
''' <summary>
''' 执行无参数存储过程返回DataTable
''' </summary>
''' <param name="ProcName">存储过程名</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function QueryProc(ByVal ProcName As String) As System.Data.DataTable Implements IDbHelper.QueryProc
Return Me.qureys(cmd, ProcName, CommandType.StoredProcedure)
End Function
''' <summary>
''' 执行带参数存储过程返回DataTable
''' </summary>
''' <param name="ProcName">存储过程名</param>
''' <param name="Parameters">参数</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function QueryProc(ByVal ProcName As String, ByVal Parameters() As IDbDataParameter) As System.Data.DataTable Implements IDbHelper.QueryProc
Return Me.qureys(cmd, ProcName, CommandType.StoredProcedure, Parameters)
End Function
''' <summary>
''' 执行带无数存储过程返回DataSet
''' </summary>
''' <param name="ProcName">存储过程名</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function QueryProcDataSet(ByVal ProcName As String) As System.Data.DataSet Implements IDbHelper.QueryProcDataSet
Return Me.qureysDataset(cmd, ProcName, CommandType.StoredProcedure)
End Function
''' <summary>
''' 执行带无数存储过程返回DataSet
''' </summary>
''' <param name="ProcName">存储过程名</param>
''' <param name="Parameters">参数名</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function QueryProcDataSet(ByVal ProcName As String, ByVal Parameters() As IDbDataParameter) As System.Data.DataSet Implements IDbHelper.QueryProcDataSet
Return Me.qureysDataset(cmd, ProcName, CommandType.StoredProcedure, Parameters)
End Function
End Class
其实为什么选择vb写数据持久层,自己也不太清楚,大概是以前一直觉得vb是数据库的亲家,那是acc时代的事情了,现在不太清楚,但是我发现一个问题,vs里(我用vs2010)写vb比写C#快,智能提示很完善,C#手动打的代码很多,VB却大多能提示出来,连try catch都是自动补全的。不知道这个可以设置成一样不,但是可以确定一点就是,VB写的注释在折叠后能显示注释的标题如下图显示:
让人看起来很方便,C#没法做到这点吗?
我是个代码狂,不太喜欢去摆弄开发工具,IED对我来说差不多就是做智能提示写代码和编译的工具,让人见笑了。
来源:https://www.cnblogs.com/ouyanghanwen/archive/2010/11/26/1888357.html