机房收费系统总结之登录功能实例解析抽象工厂+配置文件

机房收费系统合作开发已经完毕,分层的好处在系统调试阶段得到了很好的体现。尤其是抽象工厂和配置文件的使用对于提高系统的可维护性功不可没。下面通以登录功能为例总结一下机房收费系统。

这张图是整个机房收费系统的包图:

机房收费系统总结之登录功能实例解析抽象工厂+配置文件_第1张图片

                           

UI层:

     

U层负责数据的录入与输出,在U层,先通过调用U层中的验证用户类的验证方法来确认所输入用户是否合法,然后通过调用Façade层验证用户类的查询方法来确认该用户是否存在,最后通过调用Façade层的Login()方法进行登录。

        Dim myTools As New Tools
        myTools.CheckNull(Me)

        Dim user As New Entity.UserInfo
        user.PWD = txtPWD.Text.Trim
        user.UserID = txtUserName.Text.Trim


        Dim strResult As String
        Dim myLogFac As New Facade.LogFAC
        strResult = myLogFac.Login(user)
        Select Case strResult
            Case "此用户不存在"
                MessageBox.Show("此用户不存在,请重新输入", "警告")
                txtUserName.Focus()
                txtUserName.SelectAll()
            Case "密码不正确"
                MessageBox.Show("密码输入错误", "警告")
                txtPWD.Focus()
                txtPWD.SelectAll()
            Case "此用户已经在线"
                MessageBox.Show("此用户已经在线", "警告")
                txtUserName.Focus()
                txtUserName.SelectAll()
            Case "登录成功"

                frmMain.Show()
                Me.Close()

        End Select

Facade层:

简称F层,该层的作用可以形象的比喻成“纲”,即渔网中能够提携全网的绳子。以登录系统为例,当我们正常登录时,依次应该发生验证用户输入是否合法,查询用户是否存在,(如果存在)添加用户登录记录等功能。F层中的Login()方法提中则分别依次对这个三个方法进行了调用,并且在依次调用时分别做了相应的意外处理。简单来讲就是调用Facade层中的这一个函数即实现登录功能。

Public Function Login(ByVal user As Entity.UserInfo) As String
        Dim bllConfirmUser As New BLL.ConfirmUser
        Dim bllGetUsersOnline As New BLL.GetUsersOnline

        Dim bllDateTime As New BLL.DateTimeManager
        Dim WorkLog As New Entity.WorklogInfo

        If Not bllConfirmUser.IsExists(user) Then
            Return "此用户不存在"
        Else
            If Not bllConfirmUser.ConfirmPWD(user) Then
                Return "密码不正确"
            Else
                Return "登录成功"
            End If
        End If

    End Function

B层:

B层是业务逻辑层,主要是进行逻辑判断并存放各种功能函数。对应于本层中如查询用户是否存在的函数和添加用户登录记录的函数均应写在B层中。

查询用户是否存在的函数IsExist()

Public Function IsExists(ByVal user As Entity.UserInfo) As Boolean
        Dim iuser As IUserDAO
        Dim factory As DBFactory
        factory = DBFactory.GetIstance

        iuser = factory.CreateUserDAO
        If iuser.QueryUserInfo(user).UserID = "" Then
            Return False
        Else
            Return True
        End If

    End Function

Factory层:

工厂层的主要作用是应用配置文件和反射技术实现数据库的更换功能。在Factory层中首先定义程序集的名字和明明空间的名字,将程序集的名字和命名空间的值写在配置文件中,当执行到Factory函数时程序会自动通过读取配置文件中的相应字符,按照路径实例化出相应的对象。

Dim strDB As String = ConfigurationManager.AppSettings("DB")
    Dim strSpace As String = strDB & "DAL"
    Dim strClassName As String
    Public Function CreateUserDAO() As IDAL.IUserDAO
        strClassName = strSpace + "." + "BasicDataDAO"
        Return CType(Assembly.Load(strSpace).CreateInstance(strClassName), IUserDAO)
    End Function

在上面的代码中"DB"的值为使用的相应数据库的名字,在本程序中明明空间的名字和程序集的名字相同,所以没有灵性设置。通过将使用的数据库的名字用配置文件中key值来代替便可以十分方便的更换数据库。另外在上述代码中将实例化的D层类通过向上转型转换成接口类,然后通过调用接口类中的函数来调用D层中实现该接口的函数。

IDAL层:

IDAL层中主要放置接口函数:

Public Interface IUserDAO
    Function AddRecord(ByVal user As Entity.UserInfo) As Boolean

    Function DeleteRecord(ByVal user As Entity.UserInfo) As Boolean

    Function UpdatePWD(ByVal user As Entity.UserInfo) As Boolean

    Function QueryOperator() As DataTable

    Function QueryUserInfo(ByVal user As Entity.UserInfo) As Entity.UserInfo

    Function QueryUserbyLevel(ByVal user As Entity.UserInfo) As DataTable
End Interface

D层:

D层通过向SqlHelper中传递sql语句来实现增删改查等各项功能:

 ''' <summary>
    ''' 获取一条用户信息记录
    ''' </summary>
    ''' <param name="user"></param>
    ''' <returns>Entity.UserInfo</returns>
    ''' <remarks></remarks>
    Public Function QueryUserInfo(ByVal user As Entity.UserInfo) As Entity.UserInfo Implements IDAL.IUserDAO.QueryUserInfo
        Dim table As DataTable
        Dim aUser As New Entity.UserInfo
        Dim strSQL As String = "select * from T_UserInfo where userid=@UserID and isdeleted='否'"
        Dim paras As SqlParameter() = {New SqlParameter("@UserID", user.UserID)}



        table = SqlHelper.DBHelper.GetDataTable(strSQL, CommandType.Text, paras)
        If table.Rows.Count <> 0 Then

            aUser.UserID = table.Rows(0).Item("UserID")
            aUser.PWD = table.Rows(0).Item("pwd")
            aUser.Level = table.Rows(0).Item("Level")
            aUser.UserName = table.Rows(0).Item("UserName")

            aUser.IsDeleted = table.Rows(0).Item("isdeleted")

        End If





        Return aUser

    End Function

DBHelper层:

执行增删改查等功能,sqlhelper中的主要函数包括获得执行带参数和不带参数的非查询功能,执行带参数的和不带参数的获取datatable的查询功能,获取带参数的和不带参数的获得一个阅读器的查询功能:

Public Class DBHelper

    ''' <summary>
    ''' 执行不带参数的非查询操作
    ''' </summary>
    ''' <param name="cmdTxt">增删改SQL语句或者存储过程名称</param>
    ''' <param name="cmdType">命令类型(文本或者存储过程)</param>
    ''' <returns>受影响的行数</returns>
    ''' <remarks></remarks>
    Public Shared Function ExecuteNoQuery(ByVal cmdTxt As String, ByVal cmdType As CommandType) As Integer
        'Dim strCnn As String = "Data Source=.;Initial Catalog=Charge_Sys;User ID=sa;pwd=123456"     '定义一个连接字符串

        '改:更改连接字符串为如下,因为用上面的连接字符串连接我电脑上的数据库不成功  杨长收
        Dim strCnn As String = "Data Source=.\SQLSERVER2008;Initial Catalog=Charge_Sys;User ID=sa;Password=123456"
        Dim Cnn As New SqlConnection(strCnn) '定义一个数据库连接对象
        Dim cmd As SqlCommand    '定义一个命令对象
        
        Dim res As Integer  '定义一个变量用于存放返回结果

        cmd = New SqlCommand(cmdTxt, Cnn)
        cmd.CommandType = cmdType


        Try
            '打开数据库连线
            If Cnn.State = ConnectionState.Closed Then
                Cnn.Open()
            End If


            '执行命令
            res = cmd.ExecuteNonQuery()




        Catch ex As Exception
            MsgBox(ex.Message, , "数据库操作")
        Finally

            '关闭数据库连线
            If Cnn.State = ConnectionState.Open Then
                Cnn.Close()

            End If
        End Try

        Return res  '返回受影响的行数
    End Function

    ''' <summary>
    ''' 执行带参数的费查询操作
    ''' </summary>
    ''' <param name="cmdTxt">增删改SQL语句或者存储过程名称</param>
    ''' <param name="cmdType">命令类型(文本或者存储过程)</param>
    ''' <param name="paras">参数数组</param>
    ''' <returns>受影响的行数</returns>
    ''' <remarks></remarks>
    Public Shared Function ExecuteNoQuery(ByVal cmdTxt As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As Integer
        'Dim strCnn As String = "Data Source=.;Initial Catalog=Charge_Sys;User ID=sa;pwd=123456"     '定义一个连接字符串

        '更改连接字符串为如下,因为用上面的连接字符串连接我电脑上的数据库不成功
        Dim strCnn As String = "Data Source=.\SQLSERVER2008;Initial Catalog=Charge_Sys;User ID=sa;Password=123456"
        Dim Cnn As New SqlConnection(strCnn) '定义一个数据库连接对象
        Dim cmd As SqlCommand    '定义一个命令对象
        
        Dim res As Integer  '定义一个变量用于存放返回结果


        cmd = New SqlCommand(cmdTxt, Cnn)
        cmd.CommandType = cmdType
        cmd.Parameters.AddRange(paras)


        Try
            '打开数据库连线
            If Cnn.State = ConnectionState.Closed Then
                Cnn.Open()
            End If


            '执行命令
            res = cmd.ExecuteNonQuery()




        Catch ex As Exception
            MsgBox(ex.Message, , "数据库操作")
        Finally

            '关闭数据库连线
            If Cnn.State = ConnectionState.Open Then
                Cnn.Close()

            End If
        End Try

        Return res  '返回受影响的行数
    End Function


    ''' <summary>
    ''' 获取一个不带参数的查询的DataTable结果集,
    ''' </summary>
    ''' <param name="cmdTxt">查询SQL语句或者存储过程名称</param>
    ''' <param name="cmdType">命令类型(文本或者存储过程)</param>
    ''' <returns>查询的结果</returns>
    ''' <remarks></remarks>
    Public Shared Function GetDataTable(ByVal cmdTxt As String, ByVal cmdType As CommandType) As DataTable
        'Dim strCnn As String = "Data Source=.;Initial Catalog=Charge_Sys;User ID=sa;pwd=123456"     '定义一个连接字符串

        '更改连接字符串为如下,因为用上面的连接字符串连接我电脑上的数据库不成功
        Dim strCnn As String = "Data Source=.\SQLSERVER2008;Initial Catalog=Charge_Sys;User ID=sa;Password=123456"
        Dim Cnn As New SqlConnection(strCnn) '定义一个数据库连接对象
        Dim cmd As SqlCommand    '定义一个命令对象
        Dim adaptor As SqlDataAdapter    '定义一个适配器对象
        Dim aDataset As DataSet   '定义一个数据集
        
        cmd = New SqlCommand(cmdTxt, Cnn)
        adaptor = New SqlDataAdapter(cmd)
        aDataset = New DataSet
        cmd.CommandType = cmdType

        Try
            '打开数据库连线
            If Cnn.State = ConnectionState.Closed Then
                Cnn.Open()
            End If

            '填充数据集
            adaptor.Fill(aDataset)



        Catch ex As Exception
            MsgBox(ex.Message, , "数据库操作")

        Finally

            '关闭数据库连线
            If Cnn.State = ConnectionState.Open Then
                Cnn.Close()
            End If
        End Try

        Return aDataset.Tables(0)   '返回数据集的第一个表

    End Function


    ''' <summary>
    ''' 获取一个带参数的查询的DataTable结果集
    ''' </summary>
    ''' <param name="cmdTxt">查询SQL语句或者存储过程名称</param>
    ''' <param name="cmdType">命令类型(文本或者存储过程)</param>
    ''' <param name="paras">参数数组</param>
    ''' <returns>查询的结果</returns>
    ''' <remarks></remarks>
    Public Shared Function GetDataTable(ByVal cmdTxt As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As DataTable
        'Dim strCnn As String = "Data Source=.;Initial Catalog=Charge_Sys;User ID=sa;pwd=123456"     '定义一个连接字符串

        '更改连接字符串为如下,因为用上面的连接字符串连接我电脑上的数据库不成功
        Dim strCnn As String = "Data Source=.\SQLSERVER2008;Initial Catalog=Charge_Sys;User ID=sa;Password=123456"     '定义一个连接字符串

        Dim Cnn As New SqlConnection(strCnn) '定义一个数据库连接对象
        Dim cmd As SqlCommand    '定义一个命令对象
        Dim adaptor As SqlDataAdapter    '定义一个适配器对象
        Dim aDataset As DataSet   '定义一个数据集
        

        cmd = New SqlCommand(cmdTxt, Cnn)
        adaptor = New SqlDataAdapter(cmd)
        aDataset = New DataSet
        cmd.CommandType = cmdType
        cmd.Parameters.AddRange(paras)

        Try
            '打开数据库连线
            If Cnn.State = ConnectionState.Closed Then
                Cnn.Open()
            End If

            '填充数据集
            adaptor.Fill(aDataset)



        Catch ex As Exception
            MsgBox(ex.Message, , "数据库操作")

        Finally

            '关闭数据库连线
            If Cnn.State = ConnectionState.Open Then
                Cnn.Close()
            End If
        End Try

        Return aDataset.Tables(0)    '返回数据集的第一个表
    End Function


    ''' <summary>
    ''' 获取一个不带参数的查询结果阅读器
    ''' </summary>
    ''' <param name="cmdTxt">查询的SQL语句或者存储过程名称</param>
    ''' <param name="cmdType">命令类型(文本或者存储过程)</param>
    ''' <returns>查询结果</returns>
    ''' <remarks></remarks>
    Public Shared Function GetReader(ByVal cmdTxt As String, ByVal cmdType As CommandType) As SqlDataReader

        'Dim strCnn As String = "Data Source=.;Initial Catalog=Charge_Sys;User ID=sa;pwd=123456"     '定义一个连接字符串

        '更改连接字符串为如下,因为用上面的连接字符串连接我电脑上的数据库不成功
        Dim strCnn As String = "Data Source=.\SQLSERVER2008;Initial Catalog=Charge_Sys;User ID=sa;Password=123456"
        Dim Cnn As New SqlConnection(strCnn) '定义一个数据库连接对象
        Dim cmd As SqlCommand    '定义一个命令对象





        'Dim reader As SqlDataReader

        cmd = New SqlCommand(cmdTxt, Cnn)
        cmd.CommandType = cmdType

        Try
            '打开数据库连线
            If Cnn.State = ConnectionState.Closed Then
                Cnn.Open()
            End If

            '执行命令

            'reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        Catch ex As Exception
            MsgBox(ex.Message, , "数据库操作")
        Finally


        End Try

        '返回一个阅读器


        Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
    End Function


    ''' <summary>
    ''' 获取一个带参数的查询结果阅读器
    ''' </summary>
    ''' <param name="cmdTxt">查询的SQL语句或者存储过程名称</param>
    ''' <param name="cmdType">命令类型(文本或者存储过程)</param>
    ''' <param name="paras">参数数组</param>
    ''' <returns>查询结果</returns>
    ''' <remarks></remarks>




    Public Shared Function GetReader(ByVal cmdTxt As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As SqlDataReader

        'Dim strCnn As String = "Data Source=.;Initial Catalog=Charge_Sys;User ID=sa;pwd=123456"     '定义一个连接字符串

        '更改连接字符串为如下,因为用上面的连接字符串连接我电脑上的数据库不成功
        Dim strCnn As String = "Data Source=.\SQLSERVER2008;Initial Catalog=Charge_Sys;User ID=sa;Password=123456"
        Dim Cnn As New SqlConnection(strCnn) '定义一个数据库连接对象
        Dim cmd As SqlCommand    '定义一个命令对象
        


        cmd = New SqlCommand(cmdTxt, Cnn)
        cmd.CommandType = cmdType
        cmd.Parameters.AddRange(paras)
        'Dim reader As SqlDataReader
        Try
            '打开数据库连线
            If Cnn.State = ConnectionState.Closed Then
                Cnn.Open()
            End If

            '执行命令

            'reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        Catch ex As Exception
            MsgBox(ex.Message, , "数据库操作")
        Finally


        End Try

        '返回一个阅读器
        Return cmd.ExecuteReader()

    End Function

  

End Class


你可能感兴趣的:(数据库,exception,function,String,sqlserver,dataset)