“就不能不换DB吗?——抽象工厂模式”,没错,这是大话设计中的一章,在这一章中,大鸟教会了小菜,用配置文件改变‘DB’的值,从而改变了应用程序要访问的数据库类型。详情请见大话设计模式P157
因为我们使用的是应用程序配置文件,所以我们将配置文件添加在UI层,右击UI层->新建项->常规->应用程序配置文件,在app.config中对connstr进行赋值。
- sqlHelper将我们日常生活中常用的四个存储过程(insert,delete,modify, select)进行了封装
- 有参,返回值为sqlDataReader的查方法
<span style="font-family:Tahoma;font-size:18px;">Imports System Imports System.Data '需要在解决方案管理器中对sqlHelper所在的项目中添加对System.Configuration的引用 Imports System.Configuration Imports System.Data.sqlClient Public Class sqlHelper Dim conn As sqlConnection Dim cmd As sqlCommand Dim connstr As String = ConfigurationManager.AppSettings("connstr").ToString Public sqlHelper() #Region "建立数据库连接对象" ''' <summary> ''' 建立数据库的连接 ''' </summary> ''' <returns>返回一个数据库的连接sqlConnection对象</returns> ''' <remarks></remarks> Public Function getsqlConnection() As sqlConnection Try conn = New sqlConnection(connstr) '利用配置文件,连接到connstr所指定的数据库 If (conn.State.Equals(ConnectionState.Open)) = False Then conn.Open() End If Catch ex As Exception Throw New Exception(ex.Message.ToString()) End Try Return conn End Function #End Region #Region "设置sqlCommand对象" ''' <summary> ''' 设置sqlCommand对象 ''' </summary> ''' <param name="cmd">sqlCommand对象</param> ''' <param name="cmdText">命令文本</param> ''' <param name="cmdType">命令类型</param> ''' <param name="cmdParms">参数集合</param> ''' <remarks></remarks> Public Sub SetCommand(ByVal cmd As sqlCommand,ByVal cmdText As String,ByVal cmdType As CommandType,ByVal cmdParms As sqlParameter()) cmd.Connection = conn cmd.CommandText = cmdText cmd.CommandType = cmdType If (cmdParms Is Nothing) = False Then cmd.Parameters.AddRange(cmdParms) End If End Sub #End Region #Region "无参,返回所影响的行数的增删改方法" ''' <summary> ''' 执行不带参数sql语句或存储过程,返回所影响的行数 ''' </summary> ''' <param name="cmdText">增,删,改sql语句</param> ''' <returns>返回所影响的行数</returns> ''' <remarks></remarks> Public Function ExecuteNonQuery(ByVal cmdText As String) As Int16 Dim count As Int16 Try getsqlConnection() cmd = New sqlCommand(cmdText,conn) count = cmd.ExecuteNonQuery() conn.Close() Catch ex As Exception Throw New Exception(ex.Message.ToString()) End Try Return count End Function #End Region #Region "有参,返回所影响的行数的增删改方法" ''' <summary> ''' 执行带参数sql语句或存储过程,返回所影响的行数 ''' </summary> ''' <param name="cmdText">带参数的sql语句和存储过程名</param> ''' <param name="cmdType">命令类型</param> ''' <param name="cmdparms">参数集合</param> ''' <returns>返回所影响的行数</returns> ''' <remarks></remarks> Public Function ExecuteNonQuery(ByVal cmdText As String,ByVal cmdparms As sqlParameter()) Dim count As Int16 Try getsqlConnection() Dim cmd As New sqlCommand SetCommand(cmd,cmdText,cmdType,cmdparms) count = cmd.ExecuteNonQuery() cmd.Parameters.Clear() conn.Close() Catch ex As Exception Throw New Exception(ex.Message.ToString) End Try Return count End Function #End Region #Region "无参,具有返回值的查询方法" ''' <summary> ''' 执行不带参数sql语句,返回一个从数据源读取数据的sqlDataReader对象 ''' </summary> ''' <param name="cmdText">相应的sql语句</param> ''' <returns>一个从数据源读取数据的sqlDataReader对象</returns> ''' <remarks></remarks> Public Function ExecuteReader(ByVal cmdText As String) As sqlDataReader Dim reader As sqlDataReader Try getsqlConnection() cmd = New sqlCommand(cmdText,conn) reader = cmd.ExecuteReader(CommandBehavior.CloseConnection) Catch ex As Exception Throw New Exception(ex.Message.ToString) End Try Return reader End Function #End Region #Region "无参,具有返回值的查询方法" ''' <summary> ''' 执行带参数的sql语句或存储过程,返回一个从数据源读取数据的sqlDataReader对象 ''' </summary> ''' <param name="cmdText">sql语句或存储过程名</param> ''' <param name="cmdType">命令类型</param> ''' <param name="cmdParms">参数集合</param> ''' <returns>返回一个从数据源读取数据的sqlDataReader对象</returns> ''' <remarks></remarks> Public Function ExecuteReader(ByVal cmdText As String,ByVal cmdParms As sqlParameter()) As sqlDataReader Dim reader As sqlDataReader Try getsqlConnection() cmd = New sqlCommand() SetCommand(cmd,cmdParms) reader = cmd.ExecuteReader(CommandBehavior.CloseConnection) Catch ex As Exception Throw New Exception(ex.Message.ToString()) End Try Return reader End Function #End Region End Class</span>
- 有参,返回值为sqlDataReader的查方法
有参,返回值为所影响行数的增删改使用
从开始知道有个sqlHelper到真正实现,花了我一周的时间,可真是耗时耗力,不过我也体会到了sqlHelper的好处,sqlHelper简化了我们需要重复写的数据库连接,如sqlConnection,sqlCommand,sqlDataReader等等;还有,sqlHelper封装过后通常是只需要给方法传入一些参数如数据库连接字符串,sql参数等,就可以访问数据库了,很方便。