VB.NET 简单三层登录实例

[url]http://blog.csdn.net/wangxuhebeibd/article/details/9104383[/url]
三层实例是以传实体层中的实体为例子的。UI层引用BLL层和Entity层,BLL层引用DAL层和Entity层,DAL层引用Entity层。
实体层(Entity)创建数据库中的实体类名称为EntityUserinfo。
BLL层创建名称为LoginBLL的类库,DAL层创建名称为LoginDAL的类库。
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值