【VB.NET机房重构】.NET三层登录

2024-08-26 02:32
文章标签 登录 重构 net 三层 vb 机房

本文主要是介绍【VB.NET机房重构】.NET三层登录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

         重构这么久,刚摸出点门道,先来介绍一下.NET三层登录。

一、分析

       1、逻辑分析


2、前提准备



3、登录界面


二、代码实现


Entity层代码(UserInfoEntity和WorkLogEntity)

Public Class UserInfoEntity'定义变量Private struserID As StringPrivate strpassword As StringPrivate strlevel As StringPrivate strstatus As StringPublic Property UserID() As StringGetReturn struserIDEnd GetSet(value As String)struserID = valueEnd SetEnd PropertyPublic Property Password() As StringGetReturn strpasswordEnd GetSet(value As String)strpassword = valueEnd SetEnd PropertyPublic Property Level() As StringGetReturn strlevelEnd GetSet(value As String)strlevel = valueEnd SetEnd PropertyPublic Property Status() As StringGetReturn strstatusEnd GetSet(value As String)strstatus = valueEnd SetEnd Property
End Class

Public Class WorkLogEntityPrivate struserID As StringPrivate strloginTime As StringPrivate strlogoutTime As StringPrivate strstatus As StringPublic Property UserID() As StringGetReturn struserIDEnd GetSet(value As String)struserID = valueEnd SetEnd PropertyPublic Property LoginTime() As StringGetReturn strloginTimeEnd GetSet(value As String)strloginTime = valueEnd SetEnd PropertyPublic Property LogoutTime() As StringGetReturn strlogoutTimeEnd GetSet(value As String)strlogoutTime = valueEnd SetEnd PropertyPublic Property Status() As StringGetReturn strstatusEnd GetSet(value As String)strstatus = valueEnd SetEnd Property
End Class
DAL层代码(UserInfoDAL和WorkLogDAL)

Imports System.Data.SqlClient
Imports System.Data
Public Class UserInfoDAL''' <summary>''' 查询用户''' </summary>''' <param name="userinfoentity1"></param>''' <returns></returns>''' <remarks></remarks>Function SelectUser(ByVal userinfoentity1 As Entity.UserInfoEntity)Dim conn As New SqlConnection '创建连接对象Dim cmd As New SqlCommand '创建命令对象conn = New SqlConnection(Dbutil.connstring()) '连接数据库cmd.Connection = conn '获取SQL语句的内容Dim reader As SqlDataReader '定义SqlDataReader的变量reader'传递数据cmd.Parameters.Add(New SqlParameter("@UserID", userinfoentity1.UserID))cmd.Parameters.Add(New SqlParameter("@Password", userinfoentity1.Password))'定义SQL语句cmd.CommandText = "Select * from UserInfo where UserID=@UserID and Password=@Password and Status='True'"cmd.CommandType = CommandType.Textconn.Open() '打开数据库链接reader = cmd.ExecuteReader() '执行查询语句,生成一个DataReaderDim userinfoentity3 As New Entity.UserInfoEntity'读取查询到的数据,返回相应属性While reader.Read()userinfoentity3.UserID = reader.GetString(0)userinfoentity3.Password = reader.GetString(1)End WhileReturn userinfoentity3End Function
End Class

Imports System.Data.SqlClient
Imports System.Data
Public Class WorkLogDALPublic Function InsertUser(ByVal worklogentity As Entity.WorkLogEntity) As BooleanDim conn As New SqlConnection '创建连接对象Dim cmd As New SqlCommand '创建命令对象conn = New SqlConnection(Dbutil.connstring()) '连接数据库cmd.Connection = conn'传入数据cmd.Parameters.Add(New SqlParameter("@UserID", Entity.Common2.strCurrentUserID))cmd.Parameters.Add(New SqlParameter("@Status", True))worklogentity.LoginTime = Format(Now, "yyyy-mm-dd hh:mm:ss")cmd.Parameters.Add(New SqlParameter("@LoginTime", worklogentity.LoginTime))'定义SQL语句cmd.CommandText = "Insert into WorkLog(UserID,LoginTime,Status)values(@UserID,@LoginTime,@Status)"conn.Open()Dim num As Integer '表示插入命令的结果Dim flag As New Booleannum = cmd.ExecuteNonQuery '执行插入命令If num > 0 Thenflag = TrueElseflag = FalseEnd IfReturn flagEnd Function
End Class
BLL层代码(LoginBLL和AddWorkLogBLL)

Public Class LoginBLLPublic Function UserLogin(ByVal userinfoentity2 As Entity.UserInfoEntity) As Entity.UserInfoEntityDim userinfodal As New DAL.UserInfoDAL '实例化UserInfoDALDim userinfoentity4 As Entity.UserInfoEntity '定义一个UserInfoEntity的参数userinfoentity4 = userinfodal.SelectUser(userinfoentity2)'判断是否查询到记录If IsNothing(userinfoentity4.UserID) ThenMsgBox("登录失败,用户名和密码不正确!")ElseMsgBox("登录成功,进入系统!")End IfReturn userinfoentity4End Function
End Class

Public Class AddWorkLogBLLPublic Function AddWorkLog(ByVal worklogentity As Entity.WorkLogEntity) As BooleanDim worklogdal As New DAL.WorkLogDAL '实例化WorkLogDALDim flag As Boolean 'flag用于定义DAL层的方法WorkLog的返回结果flag = worklogdal.InsertUser(worklogentity)Return flagEnd Function
End Class
UI层代码

Public Class frmLoginPrivate Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.ClickDim userinfoentity As New Entity.UserInfoEntity '实例化UserInfoEntityDim worklogeneity As New Entity.WorkLogEntity '实例化WorkLogEntityDim userinfoentity5 As Entity.UserInfoEntity '定义一个实体层的参数'将界面中的数据传给实体层userinfoentity.UserID = txtUserID.Text.Trim()userinfoentity.Password = txtPassword.Text.Trim()'判断输入框是否为空If txtUserID.Text = "" ThenMessageBox.Show("用户名不能为空")ReturnEnd IfIf txtPassword.Text = "" ThenMessageBox.Show("密码不能为空")ReturnEnd If'调用B层,进行判断Dim loginbll As New BLL.LoginBLL '实例化LoginBLLuserinfoentity5 = loginbll.UserLogin(userinfoentity)'将正在登录的用户登录信息传给全局变量Dim strcommon As New Entity.Common2strcommon.CurrentUserID = txtUserID.Text.Trim()strcommon.CurrentPassword = txtPassword.Text.Trim()Dim flag As BooleanDim addworklogbll As New BLL.AddWorkLogBLL '实例化AddWorkLogBLLflag = addworklogbll.AddWorkLog(worklogeneity)Me.Visible = FalsefrmMain.Visible = TrueEnd Sub
End Class
运行结果:



总结:三层虽然很简单,但是也要认真的搞清楚,这是后面重构的基础。不管会不会,一定要不停的先去做,再不行先照着别人的敲出来,搞清楚各层的调用和跳转再自己重新敲,肯定会有收获。

这篇关于【VB.NET机房重构】.NET三层登录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1107297

相关文章

Security OAuth2 单点登录流程

单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自独立的软件系统,提供访问控制的属性。当拥有这项属性时,当用户登录时,就可以获取所有系统的访问权限,不用对每个单一系统都逐一登录。这项功能通常是以轻型目录访问协议(LDAP)来实现,在服务器上会将用户信息存储到LDAP数据库中。相同的,单一注销(single sign-off)就是指

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

【测试】输入正确用户名和密码,点击登录没有响应的可能性原因

目录 一、前端问题 1. 界面交互问题 2. 输入数据校验问题 二、网络问题 1. 网络连接中断 2. 代理设置问题 三、后端问题 1. 服务器故障 2. 数据库问题 3. 权限问题: 四、其他问题 1. 缓存问题 2. 第三方服务问题 3. 配置问题 一、前端问题 1. 界面交互问题 登录按钮的点击事件未正确绑定,导致点击后无法触发登录操作。 页面可能存在

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get

2、PF-Net点云补全

2、PF-Net 点云补全 PF-Net论文链接:PF-Net PF-Net (Point Fractal Network for 3D Point Cloud Completion)是一种专门为三维点云补全设计的深度学习模型。点云补全实际上和图片补全是一个逻辑,都是采用GAN模型的思想来进行补全,在图片补全中,将部分像素点删除并且标记,然后卷积特征提取预测、判别器判别,来训练模型,生成的像

Shell脚本实现自动登录服务器

1.登录脚本 login_server.sh #!/bin/bash# ReferenceLink:https://yq.aliyun.com/articles/516347#show all host infos of serverList.txtif [[ -f ./serverList.txt ]]thenhostNum=`cat ./serverList.txt | wc -l`e

用VB创建开始菜单快捷方式(无需其他DLL)

Option Explicit   Private Sub Command1_Click()   CreateProgManGroup Me, "测试", "test.grp"   CreateProgManItem Me, "d:\ghost.exe", "Ghost"   CreateProgManItem Me, "d:\setupQQ.exe", "QQ"   End

.NET 自定义过滤器 - ActionFilterAttribute

这个代码片段定义了一个自定义的 ASP.NET Core 过滤器(GuardModelStateAttribute),用于在控制器动作执行之前验证模型状态(ModelState)。如果模型状态无效,则构造一个 ProblemDetails 对象来描述错误,并返回一个 BadRequest 响应。 代码片段: /// <summary>/// 验证 ModelState 是否有效/// </

VB和51单片机串口通信讲解(只针对VB部分)

标记:该篇文章全部搬自如下网址:http://www.crystalradio.cn/thread-321839-1-1.html,谢谢啦            里面关于中文接收的部分,大家可以好好学习下,题主也在研究中................... Commport;设置或返回串口号。 SettingS:以字符串的形式设置或返回串口通信参数。 Portopen:设置或返回串口

VB项目中必需的几点技巧

1.    点击右上角的关闭按钮,要弹出“提示”,是否关闭,但用右键关闭时,不能重复提示 在vb中找到这个事件Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)If MsgBox("是否要退出", vbYesNo + vbDefaultButton2, "提示") = vbNo ThenCancel