获得服务器时间--机房重构

  如何获得服务器时间!很容易,就是用SQL语句就OK了。

SQL="SELECT GETDATE()"

  很简单用SQL 语句就可以完成这个功能,现在感觉很简单,但是开始写7层读服务器时间可是伤透了脑筋。
  困难一 : 如何从数据库中获得时间 ?
  解决:SQL语句 用函数就能获得。
  困难二:写成7层,咋写呀! SQL语句查服务器表中内容会,但是是有一个条件(参数)来查的,查时间什么也没有咋查?
  解决:用无参数的SQL语句来查询。
  困难三:如何从服务器获得想要格式的时间?
  解决:查询SQL日期格式化函数。
  

  IDAL层

Public Interface IGetTime
    '定义查找服务器时间接口,返回结果类型为Datatable 
    Function GetTime(ByVal time As Entity.TimeEntity) As DataTable
End Interface

  DAL层

Public Class GetTimeDAL : Implements IDAL.IGetTime
    '实现接口中定义的方法 获取服务器时间  
    Public Function GetTime(time As TimeEntity) As DataTable Implements IGetTime.GetTime
        '实例化SQLhelper层
        Dim sqlhelper As New SQLHelper.SqlHelper
        Dim cmdType As CommandType = New CommandType()
        '声明并实例化参数数组
        ''Dim sqlParams As SqlParameter() = {New SqlParameter("@UserID", AdminInfo.AdminID), New SqlParameter("@Password", AdminInfo.Password)}

        Dim cmdtxt As String             '定义接受SQL语句的变量
        cmdtxt = "SELECT GETDATE()"      '给变量赋值SQL语句

        Dim table As New DataTable
        table = sqlhelper.ExecSelectNo(cmdtxt, CommandType.Text)'使用SQLHelper层中的无参数查询方法
        Return table
    End Function
End Class

  Factory层

Public Class GetTimeFactory
    Dim strDB As String = System.Configuration.ConfigurationManager.AppSettings("DB") '读取配置文件
        '要实例化的D层的名称  
        Dim classname As String = strDB + "." + "GetTimeDAL"
        '反射的关键代码(返回接口)
        Dim ITime As IDAL.IGetTime
        'CType函数将返回表达式显示的转换为指定的数据类型、对象、结构、类或接口后的结果          
        ITime = CType(Assembly.Load(strDB).CreateInstance(classname), IDAL.IGetTime) '返回LoginIUserInfo  
        Return ITime
    End Function
End Class

  BLL层

ublic Class GetTimeBLL
    '获得服务器时间  
    Public Function ExecTime(ByVal time As Entity.TimeEntity) As DataTable
        Dim Factory As New Factory.GetTimeFactory()  '定义工厂并实例化工厂变量factory
        Dim ITime As IDAL.IGetTime                   '定义接口变量IUser    
        ITime = Factory.CreateTime                    '调用工场方法

        Dim table As DataTable                       '中间变量,用于存储D层查询到的数据 
        table = ITime.GetTime(time)                  '调用接口的方法GetTime
        Return table                                 '结果返回值table 
    End Function
End Class

  Facade层

Public Class GetTimeFacade
    Public Function GetTime(ByVal time As Entity.TimeEntity) As DataTable
        Dim isGetTime As New BLL.GetTimeBLL()   '实例化BLL层方法

        Dim table As DataTable
        table = isGetTime.ExecTime(time)        '调用BLL层方法
        Return table
    End Function
End Class

  UI层

'获取服务器时间就用主窗体加载时间吧!
    Private Sub FrmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load    
        '激活时间控件
        TimeSQL.Interval = 10      '每10毫秒更新一次
        TimeSQL.Start()            '开启时间
    End Sub
    ''' <summary>
    ''' 获取服务器时间
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub TimeSQL_Tick(sender As Object, e As EventArgs) Handles TimeSQL.Tick      '时钟事件
        '获取数据库时间
        Dim facadeLogin As New Facade.GetTimeFacade
        Dim time As New Entity.TimeEntity      '实例化实体没有任何参数

        Dim table As New DataTable     '定义接收返回变量
        table = facadeLogin.GetTime(time)
        DayTime = table.Rows(0).Item(0)
        If IsDate(table.Rows(0).Item(0)) = True Then
           '在控件上显示时间
            FrmAdminMian.txtTime.Text = Format(table.Rows(0).Item(0), "HH:mm:ss")
            FrmAdminMian.txtDate.Text = Format(table.Rows(0).Item(0), "yyyy/MM/dd")
        Else
            Msgbox("查询时间有误")
        End If
    End Sub

  使用上述7层就能查询无参数的服务器时间了。

猜你喜欢

转载自blog.csdn.net/m18330808841/article/details/80548966