【前言】
又三层敲了一遍机房登录,感觉还比较好理解,运用还可以,但是到了七层,就有点手忙脚乱了,静下来,慢慢捋思路,终于有了雏形。七层无非是在三层的基础上加上设计模式和SQLHelper。今天对SQLHelper有了一些自己的理解。SQLhelper 字面意思就是SQL Server数据库的一个帮助,它封装了一个方法,来执行数据库的更新。SQLhelper通过SQL参数传递即可连接数据库,实现查新。其中传参的几个常用方法主要有:
ExecuteNonQuery:此方法用于执行不返回任何行或值的命令。这些命令通常用于执行数据库更新,但也可用于 返回存储过程的输出参数。 ExecuteReader:此方法用于返回SqlDataReader对象,该对象包含由某一命令返回的SqlDataReader。 ExecuteDataset:此方法返回DataSet对象,该对象包含由某一命令返回的DataSet。(使用较多的是Datatable, DataSet是由多个DataTable构成的。)
接下来,就展示一下SQLhelper这个类的代码吧:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Public Class sqlhelper
'获得数据库连接字符串
Private ReadOnly strConn As String = ConfigurationManager.AppSettings("Connstr")
'新建连接
Dim connSQL As SqlConnection = New SqlConnection(strConn)
'定义命令
Dim cmdSQL As New SqlCommand
''' <summary>
''' 执行增删改,有参,确认是否执行成功
''' </summary>
''' <param name="cmdSQLText">需要执行的数据库语句</param>
''' <param name="cmdSQLType">数据语句的类型,可能是sql类型,也可能是存储过程等</param>
''' <param name="sqlParams">参数数组,无法确定有多少参数</param>
''' <returns>返回受影响行数</returns>
''' <remarks></remarks>
Public Function ExecuteAddDelUpdate(ByVal cmdSQLText As String, ByVal cmdSQLType As CommandType, ByVal sqlParams As SqlParameter()) As Integer
cmdSQL.Parameters.AddRange(sqlParams) '将参数传入
cmdSQL.CommandType = cmdSQLType '确定语句类型,是sql还有oracle还是其他类型
cmdSQL.CommandText = cmdSQLText '将sql语句传给cmdSQL
cmdSQL.Connection = connSQL '设置连接,全局变量
'开始执行查询
Try
connSQL.Open() '打开连接
Return cmdSQL.ExecuteNonQuery() '执行查询
cmdSQL.Parameters.Clear() '清除参数
Catch ex As Exception
Return 0
Finally
Call CloseConnection(connSQL)
Call CloseSQLCommand(cmdSQL)
End Try
End Function
''' <summary>
''' 执行增删改,有参,确认是否执行成功
''' </summary>
''' <param name="cmdText">需要执行的数据库语句</param>
''' <param name="cmdType">数据语句的类型,一般为Sql语句,不是存储过程</param>
''' <returns>返回受影响的行数</returns>
''' <remarks></remarks>
Public Function ExecuteAddDelUpdate(ByVal cmdText As String, ByVal cmdType As CommandType) As Integer
cmdSQL.CommandType = cmdType '确定语句类型,是sql还有oracle还是其他类型
cmdSQL.CommandText = cmdText '将sql语句传给cmdSQL
cmdSQL.Connection = connSQL '设置连接,全局变量
'开始执行查询
Try
connSQL.Open() '打开连接
Return cmdSQL.ExecuteNonQuery() '执行查询
cmdSQL.Parameters.Clear() '清除参数
Catch ex As Exception
Return 0
Finally
Call CloseConnection(connSQL)
Call CloseSQLCommand(cmdSQL)
End Try
End Function
''' <summary>
''' 执行查询操作,有参,返回datatable类型
''' </summary>
''' <param name="cmdText">需要执行的数据库语句,一般SQL语句,也有存储过程</param>
''' <param name="cmdType">数据语句的类型,一般为Sql语句,不是存储过程</param>
''' <param name="sqlParams">参数数组,不确定有多少参数数组</param>
''' <returns>返回datatable类型</returns>
''' <remarks></remarks>
Public Function ExecuteSelect(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable
Dim sqlAdapter As SqlDataAdapter
Dim dtSQL As New DataTable
Dim dsSQL As New DataSet
cmdSQL.CommandText = cmdText
cmdSQL.CommandType = cmdType
cmdSQL.Connection = connSQL
cmdSQL.Parameters.AddRange(sqlParams)
sqlAdapter = New SqlDataAdapter(cmdSQL)
Try
sqlAdapter.Fill(dsSQL)
dtSQL = dsSQL.Tables(0) 'datatable为dataSet的第一个表
cmdSQL.Parameters.Clear()
Catch ex As Exception
MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
Finally
Call CloseSQLCommand(cmdSQL)
End Try
Return dtSQL
End Function
''' <summary>
''' 执行查询操作,无参,返回datatable类型
''' </summary>
''' <param name="cmdText">需要执行的数据库语句,一般SQL语句,也有存储过程</param>
''' <param name="cmdType">数据语句的类型,一般为Sql语句,不是存储过程</param>
''' <returns>返回datatable类型</returns>
''' <remarks></remarks>
Public Function ExecuteSelect(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTable
Dim sqlAdapter As SqlDataAdapter
Dim dtSQL As New DataTable
Dim dsSQL As New DataSet
cmdSQL.CommandText = cmdText
cmdSQL.CommandType = cmdType
cmdSQL.Connection = connSQL
sqlAdapter = New SqlDataAdapter(cmdSQL) '实例化adapter
Try
sqlAdapter.Fill(dsSQL) '用dsSQL填充sqlAdapter
dtSQL = dsSQL.Tables(0)
Catch ex As Exception
Call CloseSQLCommand(cmdSQL)
End Try
Return dtSQL
End Function
''' <summary>
''' 关闭连接
''' </summary>
''' <param name="connSQL">需要关闭连接</param>
''' <remarks></remarks>
Public Sub CloseConnection(ByVal connSQL As SqlConnection)
If (connSQL.State <> ConnectionState.Closed) Then '如果没有关闭
connSQL.Close() '关闭连接
connSQL = Nothing '不指向原对象
End If
End Sub
''' <summary>
''' 关闭命令
''' </summary>
''' <param name="cmdSQL">需要关闭命令</param>
''' <remarks></remarks>
Public Sub CloseSQLCommand(ByVal cmdSQL As SqlCommand)
If Not IsNothing(cmdSQL) Then '如果存在命令
cmdSQL.Dispose() '将命令销毁
cmdSQL = Nothing
End If
End Sub
End Class
然后D层进行调用的代码:
Imports System.Data
Imports System.Data.SqlClient
Imports IDAL
Imports LoginEntity
Imports SQLHelper
Public Class SqlServerUserDAL : Implements IDAL.IUser
Public Function selectUser(user As UserEntify) As DataTable Implements IUser.SelectUser '最后应该是 selectyser 所以有错此处
Dim cmdText As String
Dim sqlHelper As New Global.SQLHelper.sqlhelper
'实例化sqlHelper
Dim dtSql As DataTable
Dim sqlParams As SqlParameter()
cmdText = "select *from User_Info where UserName=@UserName and PWD=@PWD"
sqlParams = {New SqlParameter("@UserName", user.userName), New SqlParameter("@PWD", user.PWD)}
dtSql = sqlHelper.ExecuteSelect(cmdText, CommandType.Text, sqlParams)
End Function
End Class
以上是我的一些见解,欢迎提出一些宝贵的意见,一起交流,共同进步!