机房收费系统重构(6)——上机

背景:机房收费系统个人版敲到了现在,已经快接近尾声了,激动,兴奋......都难以言表心中的滋味,这个历程真的是很艰难呀,平时的专业课,再加上最近的论文实验,周一到周五的大部分时间都在实验室“泡着”,周六周天还有各种会议要开,这段时间过来,整个人都变得不好了。最大的感触就是:不是时间管理的不好,而是根本没有时间来让自己管理,唉......说起来都是“辛酸泪”呀,还好一直坚持着,最后的最后,终于快要完成一项“艰巨”的项目了,加油!!!

机房收费系统中最最艰难的部分就是上下机了,逻辑复杂,涉及的表多,下面再来整理一下上机的思路吧!


一、流程图


机房收费系统重构(6)——上机_第1张图片


二、代码展示


1、Sqlhelper

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Reflection

Public Class SqlHelper

    '定义全局变量变量,获得数据库的连接字符串                                                            
    Private Shared ReadOnly strConnection As String = ConfigurationManager.AppSettings("connString")
    '    '定义全局变量,设置连接      
    Shared conn As SqlConnection = New SqlConnection(strConnection)
    '    '定义全局cmd命令      
    Shared cmd As New SqlCommand
    ''' <summary>      
    ''' 执行非查询操作(增删改)有参数,      
    ''' </summary>      
    ''' <param name="cmdText">执行增删改查SQL语句或者存储过程</param>      
    ''' <param name="cmdType">命令类型文本或者是存储过程</param>      
    ''' <param name="paras">参数数组,无法确认有多少参数</param>      
    ''' <returns></returns>      
    ''' <remarks></remarks>      
    Public Shared Function ExecAddDelUpdate(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As Boolean
        '将传入的值,分别为cmd的属性赋值    
        Dim result As Integer   '定义变量用来存放执行的结果  
        cmd.Parameters.AddRange(paras)   '将参数传入      
        cmd.CommandType = cmdType            '设置一个值,解释cmdText      
        cmd.Connection = conn                '设置连接,全局变量      
        cmd.CommandText = cmdText            '设置查询的语句      

        Try
            conn.Open()                      '打开连接      
            result = cmd.ExecuteNonQuery()     '执行增删改操作      
            cmd.Parameters.Clear()           '清除参数      
        Catch ex As Exception
            MsgBox(ex.Message, , "数据库操作")                        '如果出错,返回0      
        Finally
            '关闭数据连接  
            Call CloseConn(conn)
            Call CloseCmd(cmd)
        End Try
        Return result        '返回受影响的行数  
    End Function
    ''' <summary>      
    ''' 执行增删改三个操作,(无参)      
    ''' </summary>      
    ''' <param name="cmdText">执行增删改查SQL语句或者存储过程</param>      
    ''' <param name="cmdType">命令类型文本或者是存储过程</param>      
    ''' <returns>Interger,受影响的行数</returns>     
    Public Shared Function ExecAddDelUpdateNo(ByVal cmdText As String, ByVal cmdType As CommandType) As Boolean
        '为要执行的命令cmd赋值      
        cmd.CommandText = cmdText
        cmd.CommandType = cmdType
        cmd.Connection = conn

        cmd.Connection = conn
        Dim result As Integer

        '执行操作      
        Try
            conn.Open()
            result = cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message, , "数据库操作")
        Finally
            Call CloseConn(conn)
            Call CloseCmd(cmd)

        End Try
        Return result          '返回受影响的行数  
    End Function
    '''' <summary>    
    '''' 执行查询的操作,(有参),参数不限      
    '''' </summary>      
    ''' <param name="cmdText">执行增删改SQL语句或存储过程</param>      
    ''' <param name="cmdType">文本类型或存储过程</param>      
    ''' <param name="paras">传入的参数</param>      
    ''' <returns>返回为DataTable</returns>      
    ''' <remarks></remarks>      
    Public Shared Function ExecSelect(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As DataTable

        Dim sqlAdapter As SqlDataAdapter
        Dim dt As New DataTable
        Dim ds As New DataSet
        '给cmd赋值      
        cmd.CommandText = cmdText
        cmd.CommandType = cmdType
        cmd.Connection = conn
        cmd.Parameters.AddRange(paras)  '参数添加      
        sqlAdapter = New SqlDataAdapter(cmd)  '实例化adapter      
        Try
            sqlAdapter.Fill(ds)           '用adapter将dataSet填充       
            dt = ds.Tables(0)             'datatable为dataSet的第一个表      
            cmd.Parameters.Clear()        '清除参数      
        Catch ex As Exception
            Return Nothing 'MsgBox(ex.Message, , "数据库操作")
        Finally                            '销毁cmd释放资源     
            Call CloseCmd(cmd)
        End Try
        Return dt
    End Function


    ''' <summary>      
    ''' 执行查询的操作,(无参)      
    ''' </summary>      
    ''' <param name="cmdText">执行的增删该的SQL语句或存储过程</param>      
    ''' <param name="cmdType">判断Sql语句的类型,一般都不是存储过程</param>      
    ''' <returns>dataTable,查询到的表格</returns>      
    ''' <remarks></remarks>      
    Public Shared Function ExecSelectNo(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTable
        Dim sqlAdapter As SqlDataAdapter
        Dim ds As New DataSet
        '给cmd赋值      
        cmd.CommandText = cmdText
        cmd.CommandType = cmdType
        cmd.Connection = conn
        sqlAdapter = New SqlDataAdapter(cmd)  '实例化adapter      
        Try
            sqlAdapter.Fill(ds)           '用adapter将dataSet填充       
            Return ds.Tables(0)             'datatable为dataSet的第一个表      
        Catch ex As Exception
            Return Nothing
        Finally                            '销毁cmd,释放资源      
            Call CloseCmd(cmd)
        End Try
    End Function

    ''' <summary>      
    ''' 关闭连接      
    ''' </summary>      
    ''' <param name="conn">需要关闭的连接</param>      
    ''' <remarks></remarks>      
    Public Shared Sub CloseConn(ByVal conn As SqlConnection)
        If (conn.State <> ConnectionState.Closed) Then  '如果没有关闭      
            conn.Close()                    '关闭连接      
            conn = Nothing                  '不指向原对象      
        End If

    End Sub
    ''' <summary>      
    ''' 销毁命令    
    ''' </summary>      
    ''' <param name="cmd">需要关闭的命令</param>      
    ''' <remarks></remarks>      
    Public Shared Sub CloseCmd(ByVal cmd As SqlCommand)

        If Not IsNothing(cmd) Then          '如果cmd命令存在      
            cmd.Dispose()                   '销毁      
            cmd = Nothing
        End If
    End Sub
End Class</strong></span>

2、实体层(部分Student_Info代码,Online_Info,BasicData_Info类似)

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>Public Class StudentInfo
    Private Shared _Cardno As String
    Private _Studentno As String
    Private _Studentname As String
    Private _Sex As String
    Private _Department As String
    Private _Grade As String
    Private _Sclass As String
    Private _Cash As String
    Private _Explain As String
    Private _UserID As String
    Private _Status As String
    Private _Ischeck As String
    Private _Type As String
    Private _Registerdate As String
    Private _Registertime As String
    '属性Cardno
    Public Property Cardno() As String
        Get
            Return _Cardno
        End Get
        Set(value As String)
            _Cardno = value
        End Set
    End Property

    '属性Studentno
    Public Property Studentno() As String
        Get
            Return _Studentno
        End Get
        Set(value As String)
            _Studentno = value
        End Set
    End Property</strong></span>

3、接口方法层

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>Imports Entity
Public Interface IOnlineDAO
    '创建检查卡号是否存在、是否已经退卡、余额是否充足的接口方法
    Function checkcardno(ByVal card As Entity.StudentInfo) As DataTable
    '创建检查该卡号是否正在上机的接口方法
    Function checkonline(ByVal line As Entity.OnlineInfo) As DataTable
    '创建更新online表的接口方法
    Function updateonline(ByVal line As Entity.OnlineInfo) As Boolean
    '创建获取当前上机人数的接口方法
    Function AcquireNowpeople() As DataTable

End Interface</strong></span>

4、工厂+反射层

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>Public Class SqlServerFactory
    '利用反射+配置文件+抽象工厂
    Private Shared ReadOnly AssemblyName As String = "DAL"
    '定义程序集名称变量,D层命名空间的名字
    Private Shared ReadOnly db As String = ConfigurationManager.AppSettings("DB")

    '上机
    Public Function Online() As IDAL.IOnlineDAO
        Return CType(Assembly.Load(AssemblyName).CreateInstance(AssemblyName + "." + db + "OnlineDAO"), IOnlineDAO)
    End Function</strong></span>

5、D层

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>Imports IDAL
Imports Entity
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Reflection

Public Class SqlServerOnlineDAO : Implements IOnlineDAO
    '检查用户是否存在,该卡是否已经退卡,余额是否为0
    Public Function checkcardno(card As StudentInfo) As DataTable Implements IOnlineDAO.checkcardno
        Dim sql As String
        Dim table As New DataTable
        Dim paras As SqlParameter() = {New SqlParameter("@cardno", card.Cardno)}
        sql = "select * from Student_Info where Cardno=@cardno"
        table = SqlHelper.ExecSelect(sql, CommandType.Text, paras)
        Return table
    End Function

    '检查该卡是否正在上机
    Public Function checkonlie(line As Entity.OnlineInfo) As DataTable Implements IOnlineDAO.checkonline
        Dim sql As String
        Dim table As New DataTable
        Dim paras As SqlParameter() = {New SqlParameter("@cardno", line.Cardno)}
        sql = "select * from Online_Info where Cardno=@cardno"
        table = SqlHelper.ExecSelect(sql, CommandType.Text, paras)
        Return table
    End Function

    '成功上机,更新online表
    Public Function updateonline(line As Entity.OnlineInfo) As Boolean Implements IOnlineDAO.updateonline
        Dim sql As String
        Dim table As Boolean
        Dim paras As SqlParameter() = {New SqlParameter("@cardno", line.Cardno),
                                      New SqlParameter("@cardtype", line.CardType),
                                      New SqlParameter("@studentno", line.Studentno),
                                      New SqlParameter("@studentname", line.Studentname),
                                      New SqlParameter("@sex", line.Sex),
                                      New SqlParameter("@department", line.Department),
                                      New SqlParameter("@ondate", line.Ondate),
                                      New SqlParameter("@ontime", line.Ontime),
                                      New SqlParameter("@computer", line.Computer)}
        sql = "insert into Online_Info values(@cardno,@cardtype,@studentno,@studentname,@sex,@department,@ondate,@ontime,@computer)"
        table = SqlHelper.ExecAddDelUpdate(sql, CommandType.Text, paras)
        Return table
    End Function

    '获取当前上机人数
    Public Function acquire() As DataTable Implements IOnlineDAO.AcquireNowpeople
        Dim sql As String
        Dim table As New DataTable
        sql = "select * from Online_Info "
        table = SqlHelper.ExecSelectNo(sql, CommandType.Text)
        Return table
    End Function
End Class</strong></span>

6、B层

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>Imports IDAL
Imports Entity
Imports Factory
Public Class BOnLine
    '检查用户是否存在
    Public Function Checkcardno(ByVal card As Entity.StudentInfo) As Boolean
        '定义并实例化一个工厂
        Dim factory As New Factory.SqlServerFactory
        Dim icardno As IDAL.IOnlineDAO
        Dim table As New DataTable
        Dim flag As Boolean
        icardno = factory.Online
        table = icardno.checkcardno(card)
        If table.Rows.Count = 0 Then
            flag = False
        Else
            flag = True
        End If
        Return flag
    End Function
    '检查余额是否大于0
    Public Function checkcash(ByVal card As Entity.StudentInfo) As DataTable
        Dim factory As New Factory.SqlServerFactory
        Dim icard As IDAL.IOnlineDAO
        Dim table As New DataTable
        icard = factory.Online
        table = icard.checkcardno(card)
        Return table
    End Function
    '检查该卡是否退卡
    Public Function checkstatus(ByVal card As Entity.StudentInfo) As DataTable
        Dim factory As New Factory.SqlServerFactory
        Dim icard As IDAL.IOnlineDAO
        Dim table As New DataTable
        icard = factory.Online
        table = icard.checkcardno(card)
        Return table
    End Function

    '检查该卡是否正在上机
    Public Function Checkonline(ByVal line As Entity.OnlineInfo) As DataTable
        '定义并实例化一个工厂
        Dim factory As New Factory.SqlServerFactory
        Dim icardno As IDAL.IOnlineDAO
        Dim table As New DataTable
        icardno = factory.Online
        table = icardno.checkonline(line)    
        Return table
    End Function

    '上机成功,更新online表
    Public Function updateonline(ByVal line As Entity.OnlineInfo) As Boolean
        Dim factory As New Factory.SqlServerFactory
        Dim iline As IDAL.IOnlineDAO
        Dim flag As Boolean
        iline = factory.Online
        flag = iline.updateonline(line)
        If flag = True Then
            MsgBox("上机成功!")
        Else
            MsgBox("上机失败!")
        End If
        Return flag
    End Function

    '获取当前上机人数
    Public Function acquire() As DataTable
        '定义并实例化一个工厂
        Dim factory As New Factory.SqlServerFactory
        Dim ipeople As IDAL.IOnlineDAO
        Dim table As New DataTable
        ipeople = factory.Online
        table = ipeople.AcquireNowpeople       
        Return table
    End Function
End Class</strong></span>

7、外观层

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>Public Class OnLineFacade
    '检查学生卡号是否存在
    Public Function Checkcardno(ByVal card As Entity.StudentInfo) As Boolean
        Dim bllcard As New BLL.BOnLine
        Dim flag As Boolean
        flag = bllcard.Checkcardno(card)
        If flag = True Then
            Return True
        Else
            Return False
        End If
        Return flag
    End Function

    '检查余额是否大于0
    Public Function checkcash(ByVal card As Entity.StudentInfo) As DataTable
        Dim bllcard As New BLL.BOnLine
        Dim table As New DataTable
        table = bllcard.checkcash(card)
        Return table
    End Function

    '检查该卡是否退卡
    Public Function checkstatus(ByVal card As Entity.StudentInfo) As DataTable
        Dim bllcard As New BLL.BOnLine
        Dim table As New DataTable
        table = bllcard.checkstatus(card)
        Return table
    End Function

    '检查该卡是否正在上机
    Public Function Checkonline(ByVal line As Entity.OnlineInfo) As DataTable
        Dim bllcard As New BLL.BOnLine
        Dim table As New DataTable
        table = bllcard.Checkonline(line)
        Return table
    End Function

    '上机成功,更新onlineinfo表
    Public Function updateonline(ByVal line As Entity.OnlineInfo) As Boolean
        Dim update As New BLL.BOnLine
        Dim flag As Boolean
        flag = update.updateonline(line)
        Return flag
    End Function

    '获取当前上机人数
    Public Function acquire() As DataTable
        Dim bllpeople As New BLL.BOnLine
        Dim table As New DataTable
        table = bllpeople.acquire()
        Return table
    End Function
End Class</strong></span>

8、U层

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>Imports System.Environment '获取机器名

Public Class frmMain

    Private Sub bton_Click(sender As Object, e As EventArgs) Handles bton.Click
        '检查学生卡号是否存在
        '实例化实体层
        Dim card As New Entity.StudentInfo
        '将界面数据传入
        card.Cardno = Trim(txtcardno.Text)
        '判断是否输入卡号
        If txtcardno.Text = "" Then
            MsgBox("请输入卡号!")
            Return
        End If
        '定义外观层对象
        Dim facadecard As New Facade.OnLineFacade
        Dim flag1 As Boolean
        '检查卡号是否存在
        flag1 = facadecard.Checkcardno(card)
        If flag1 = False Then
            MsgBox("该卡号不存在,请重新确认!")
            txtcardno.SelectAll()
            txtcardno.Focus()
            Return
        End If

        '检查该卡是否已经退卡
        Dim table1 As New DataTable
        table1 = facadecard.checkcash(card)
        If Trim(table1.Rows(0).Item(10)) = "未使用" Then
            MsgBox("该卡已经退卡,请重新购卡后登陆!")
            txtcardno.SelectAll()
            txtcardno.Focus()
            Return
        Else
            '检查余额是否不足
            Dim table2 As New DataTable
            table2 = facadecard.checkstatus(card)
            If Trim(table2.Rows(0).Item(7)) <= 0 Then
                MsgBox("余额不足,请充值后重新登陆!")
                txtcardno.SelectAll()
                txtcardno.Focus()
                Return
            End If
        End If

        '检查该卡是否正在上机
        Dim line As New Entity.OnlineInfo

        line.Cardno = Trim(txtcardno.Text)
        Dim table3 As New DataTable
        table3 = facadecard.Checkonline(line)

        If Not table3.Rows.Count = 0 Then
            MsgBox("该卡正在上机,请重新输入卡号!")
            txtcardno.SelectAll()
            txtcardno.Focus()
            Return
        Else
            '上机成功
            Dim student As New DataTable
            student = facadecard.checkcash(card)
            txtstudentno.Text = Trim(student.Rows(0).Item(1))
            txtstuname.Text = Trim(student.Rows(0).Item(2))
            txtsex.Text = Trim(student.Rows(0).Item(3))
            txtdepartment.Text = Trim(student.Rows(0).Item(4))
            txttype.Text = Trim(student.Rows(0).Item(12))
            '上机日期和时间
            txtondate.Text = Trim(DateTime.Now.ToLongDateString)
            txtontime.Text = Trim(DateTime.Now.ToLongTimeString)
            '余额
            txtcash.Text = Trim(student.Rows(0).Item(7))

            '将界面数据传入onlineinfo表,更新数据库
            line.Cardno = Trim(txtcardno.Text)
            line.CardType = Trim(txttype.Text)
            line.Studentno = Trim(txtstudentno.Text)
            line.Studentname = Trim(txtstuname.Text)
            line.Sex = Trim(txtsex.Text)
            line.Department = Trim(txtdepartment.Text)
            line.Ondate = Trim(txtondate.Text)
            line.Ontime = Trim(txtontime.Text)
            line.Computer = Trim(Environment.MachineName) '机器名
            Dim flag2 As Boolean
            flag2 = facadecard.updateonline(line)

            '获取当前上机人数
            Dim acquirefacade As New Facade.OnLineFacade
            Dim table As New DataTable
            Dim i As Integer
            table = acquirefacade.acquire()
            i = table.Rows.Count
            lbnowpeople.Text = Val(i)

        End If
    End Sub</strong></span>
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>    'timer控件,获得当前时间
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        lbnowtime.Text = DateTime.Now.ToLongTimeString
    End Sub</strong></span>

:代码和图片有一个地方不太符合,判断卡内余额,后期在整理思路的时候,我认为只要卡内的余额大于0,就可以上机,如果按照机房收费系统第一版的那样,小于设定的最少上机金额就不能上机,那样学生的卡里有钱但是不能上机,有点坑购卡人员的感觉,不太好!


三、学习体会

    

    在敲学生上机的这个功能的时候,最最重要的是理清思路,这样你才能思路清晰的完成庞大的代码。当然,还要本着全心全意为人民服务的宗旨,这样才能设计出更好的软件!


机房收费系统重构(6)——上机_第2张图片





你可能感兴趣的:(框架,.net,重构)