VB.NET 简单三层登录实例

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

首先在vs 中创建相应的类库以及窗体,各层操作如下:

首先明确一点,我写的三层实例是以传实体层中的实体为例子的。UI层引用BLL层和Entity层,BLL层引用DAL层和Entity层,DAL层引用Entity层。

实体层(Entity)创建数据库中的实体类名称为EntityUserinfo。

UI层创建FrmLogin窗体,如图:


BLL层创建名称为LoginBLL的类库,DAL层创建名称为LoginDAL的类库。

各层代码如下:

Entity:(主要就是用于传 实体 用,贯穿与 U层 B层 和 D层,三个层)

Public Class EntityUserInfo
    '定义两个私有属性
    Private id As String
    Private password As String

    '定义属性过程,通过属性过程,允许他的类访问该属性
    Public Property UserID() As String
        Get
            Return id
        End Get
        Set(value As String)
            id = 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 btnOK_Click(sender As Object,e As EventArgs) Handles btnOK.Click

        '实例化 传 实体的 对象 userinfo
        Dim userinfo As New Entity.EntityUserInfo
        '实例化 BLL层 的对象 loginbll
        Dim LoginBll As New BLL.LoginBLL

        '将文本框中的字符串赋值给 实体对象 userinfo ,使实体对象能够带上参数
        userinfo.UserID = txtUserName.Text.ToString
        userinfo.PWD = txtPassword.Text.ToString

        '实例化一个实体层 的 model 对象
        Dim model As New Entity.EntityUserInfo

        '调用BLL层的方法(返回值为 实体对象,所有上面定义一个 实体对象model)
        '通过调用Bll层的方法,返回实体对象,比较model的属性是否与文本框一致
        model = LoginBll.UserLogin(userinfo)  
        If model.UserID = txtUserName.Text And model.PWD = txtPassword.Text Then
            Me.Hide()
            Applycation.Show()
        Else
            MessageBox.Show("登陆失败")
        End If

    End Sub

BLL层:(业务逻辑层,调用DAL层的方法,被UI层调用 即连接UI层和DAL 层)


Imports Entity.EntityUserInfo
Imports DAL.LoginDAL
Public Class LoginBLL
    '定义一个函数方法 userlogin 传递的参数为 实体层类型的变量 model 返回值为 一个实体
    Public Function UserLogin(ByVal model As Entity.EntityUserInfo) As Entity.EntityUserInfo
        '定义DAL层 的 对象LoginDAL
        Dim LoginDAL As New DAL.LoginDAL
        '定义实体层 对象  userinfo
        Dim userinfo As New Entity.EntityUserInfo
        '调用DAL层的方法,返回 实体 
        userinfo = LoginDAL.selectuser(model)
        Return userinfo
    End Function
End Class

DAL 层(数据访问层,实现数据库的连接)


Imports System.Data.sqlClient
Imports System.Data
Imports Entity.EntityUserInfo
Public Class LoginDAL

    '数据库连接,将数据库连接定义为构造函数,当实例化 LoginDAL 的时候,自动完成数据库连接
    Dim str As String = "server=.;database=Charge_System;integrated security=sspi"   '这里字符串用的是windows 验证方式,所有不需要用户名和密码的操作。
    Dim conn As New sqlConnection(str)

    '在数据库查询数据,执行命令语句
    Public Function selectuser(ByVal model As Entity.EntityUserInfo) As Entity.EntityUserInfo

        '连接数据库
        conn.Open()
        Dim sql As String = "select * from T_Users where UserID= '" & model.UserID & " ' and PWD= '" & model.PWD & "'"

        Dim cmd As New sqlCommand(sql,conn)

        '定义一个 sqlDataReader 类型的变量 reader
        Dim reader As sqlDataReader
        reader = cmd.ExecuteReader()    '由于cmd 对象的属性 EcecuteReader 返回之是一个DataReader,所以只能定义一个Datareader类型的变量来接收数据。

        '实例化一个实体层的对象 userinfo  
        Dim userinfo As New Entity.EntityUserInfo

        'reader.read 的返回值为true 执行给 userinfo 的属性赋值操作
        While (reader.Read())
            userinfo.UserID = reader.GetString(0)
            userinfo.PWD = reader.GetString(1)
        End While
        '返回 实体对象 userinfo
        Return userinfo
    End Function
End Class


三层实例到此结束,有不到之处欢迎指正。

原文链接:https://www.f2er.com/vb/258787.html

猜你在找的VB相关文章