三层实践

前端之家收集整理的这篇文章主要介绍了三层实践前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天上专业课,学习了一点东西,收获就是如何请教别人,首先问题要有水平,问问题前你对这个问题有多少了解,做了多少的准备工作,自己理解到什么程度,问题点在什么地方,如果你做到了这些很好,你会给别人很好的印象,勤奋好学虚心请教。但是你看了一下问题,什么都没做,你去问别人,别人就会说,你对这个问题有多少理解呀!没有理解,别人就会回复你,百度查一下吧!询问失败。
下面正题,三层登录实践,感谢帮助自己的师傅。
Model:

Public Class UserInfo
    '定义两个私有属性 
    Private UserID As String
    Private Password As String

    '定义属性过程,通过属性过程,允许其他类访问 
    Public Property ID As String

        Get
            Return UserID
        End Get

        Set(value As String)
            UserID = value
        End Set
    End Property

    Public Property PWD As String

        Get
            Return Password
        End Get

        Set(value As String)
            Password = value
        End Set
    End Property
End Class

UI:

Public Class frmLogin
    Private Sub btnLogin_Click(sender As Object,e As EventArgs) Handles btnLogin.Click
        '实例化传实体对象; 
        Dim LoginUser As New Login.Model.UserInfo '用来接收用户登录名和密码; 

        '实例化B层对象 
        Dim Bcheck As New Login.BLL.LoginManager

        '将文本框中的字符串送给实体对象login.user使实体对象能够带上参数; 
        LoginUser.ID = txtUserName.Text()
        LoginUser.PWD = txtPassword.Text()


        If Bcheck.selectUIandModel(LoginUser) Then
            MsgBox("恭喜你,成功了!Amazing")
        Else
            MsgBox("这也太笨了吧,连这个都做不好,怎么谈是精英?")
        End If

    End Sub
End Class

BLL:

Public Class LoginManager
    'B层业务处理逻辑:承接U层的参数和传递到D层; 
    Function selectUIandModel(ByVal user As Login.Model.UserInfo) As Boolean

        Dim DAOuser As New Login.DAL.UserDAO '实例化D层的UserDAO类 
        Dim Moderuser As New Login.Model.UserInfo '实例化实体层UserInfo类 

        Moderuser.ID = user.ID
        Moderuser = DAOuser.selectUserInfoFormUserInfoTable(Moderuser) '引用了DAL层的方法 

        If Trim(Moderuser.PWD) = user.PWD Then
            Return True
        Else
            Return False
        End If
    End Function
End Class

DAL :

Public Class UserDAO
    'server是服务器的名称,可以是local,IP地址,计算机名称
    'database是数据库名称
    'userid是登录用户名
    'pwd是登录的密码; 
    Dim sqlConnectStr As String = "server =gao;database=Login;uid=sa;pwd=123456"

    '下面可以用windows身份验证登录
    'Dim sqlConnectStr1 As String = "server=gao;database=Login;integrated Security=true" 

    Dim sqlConnect1 As sqlConnection = New sqlConnection(sqlConnectStr)

    Function selectUserInfoFormUserInfoTable(ByVal user As Login.Model.UserInfo) As Login.Model.UserInfo
        Dim sql As String = "select * from Users where Username='" & user.ID & "'"
        Dim cmd As sqlCommand = New sqlCommand(sql,sqlConnect1) '定义sqlcommand对象; 

        Dim read As sqlDataReader '定义sqldatareader对象; 

        Dim UserDataTable As New DataTable '定义一个Datatable对象; 
        Dim user1 As New Login.Model.UserInfo

        Try
            sqlConnect1.Open()
            read = cmd.ExecuteReader
            UserDataTable.Load(read)
            user1.ID = UserDataTable.Rows(@H_461_301@0)("Username")
            user1.PWD = UserDataTable.Rows(@H_461_301@0)("Password")
            Return user1


        Catch ex As Exception
            user1.PWD = ""
            Return user1
        Finally
            If Not IsNothing(sqlConnect1) Then
                sqlConnect1.Close()
            End If
        End Try
    End Function
End Class

大家在敲完代码后,一定要记得引用,不引用的话会出现错误的。引用关系看看上一篇博客

猜你在找的VB相关文章