机房重构进行了一小阶段,刚开始敲代码的时候,实现每一个功能都要在D层类中写一次数据据库连接SqlConnection,SqlCommand,SqlDataReader语句,感到相当地麻烦。遇到这个问题后,就想到了面向对象的封装性,把D层中重复的SQL语句封装起来,以提高代码的复用性。通过上网查资料,知道了SqlHelper类和其使用方法。
SqlHelper类将数据库连接(SqlConnection,SqlCommand,SqlDataReader)语句封装起来,在实现某一功能时,只需实力化SqlHelper类之后,给其中的方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。
SqlHelper类
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
'/*************************************************
'类名:SqlHelper
'作者:孟浩杰
'说明:用户对数据库操作帮助类,对数据库的连接(sqlConnection,SqlCommand,SqlDataReader)等语句的封装。
'创建日期:2015-2-6
'版本号:机房收费系统1.0
'**********************************************/
Public Class SqlHelper
'获取配置文件中的连接字符串
Public ReadOnly strSQLConnection As String = System.Configuration.ConfigurationSettings.AppSettings("ConnStr")
'定义连接
Dim cnnSQL As New SqlConnection
'定义Cmd命令
Dim cmdSQL As New SqlCommand
'///<summary>
'///depiction<该方法是SqlHelper的初