Fork me on GitHub

用户登录验证程序——VB.NET

Introduction

实现用户登录功能,UI使用WinForm提供LoginForm,然后建一个User表保存用户信息(UserName, PassWrd, UserActive, Comment),使用Linq对User表进行查询。

plainText = CryptographyUtils.Decrypt(CryptographyUtils.CreateSymmAlgoRC2(),cipherText, "JK_huangJK_huang", 128CryptographyUtils.CreateSymmAlgoRC2(),cipherText, "JK_huangJK_huang", 128);

Backgroud

使用Visual studio2008,sql server 2005开发

 

The Definition of User Table

 

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[t_UserInfo](

[UserName] [nvarchar](20) NOT NULL,

[PassWrd] [nvarchar](20) NOT NULL,

[UserActive] [bit] NOT NULL,

[Comment] [nvarchar](50) NULL,

CONSTRAINT [PK_t_UserInfo] PRIMARY KEY CLUSTERED

(

[UserName] ASC

)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

)
ON [PRIMARY]

 

 

 

Validing the user and Passing Data to Database

上传数据方法,当CheckReg.Checked = True是创建新用户,False时检验当前用户是否有效

 

 

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim loginDS As New TestInfoDataSet.t_UserInfoDataTable
Dim loginDA As New TestInfoDataSetTableAdapters.t_UserInfoTableAdapter

If CheckReg.Checked = True Then
Try
loginDA.Insert(comUser.Text.ToString(), txtPW.Text.ToString(),
True, "")
Catch ex As Exception
MessageBox.Show(
"The UserName already exist", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
comUser.Focus()
comUser.Text
= ""
Finally
Me.ClearForm()
End Try
Else
Try
loginDA.FillByUserData(loginDS, comUser.ToString(), txtPW.ToString())
Check_Details()
Catch ex As Exception
MsgBox("Invalid User Name", MsgBoxStyle.Critical, "Error")
End Try
End If

End Sub

 

 

使用Linq查询用户是否存在User表中

 

 

Private Sub Check_Details()
Dim PWord As String = Me.txtPW.Text.ToString.Trim

Dim CheckDA As New TestInfoDataSetTableAdapters.t_UserInfoTableAdapter
Dim query = From check In CheckDA.GetData _
Select check.UserName, check.PassWrd, check.UserActive _
Where UserName
= UName AndAlso PassWrd = PWord _
AndAlso UserActive = True
If query.Count() = 1 Then
MessageBox.Show(
"confirmed", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.ClearForm()
Else
MessageBox.Show(
"Password Is Incorrect or no longer Valid", "Incorrect Password", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtPW.Focus()
txtPW.Text
= ""

End If

End Sub

 

 

Data Valide

进行输入操作时是全角输入就要转换为半角输入,这里使用两种方法进行全角到半角的转换

 

1.处理输入后的数据2.只输入半角字符

Private Function ToDBC(ByVal tmpStr As String) As String
Dim i As Integer = 0

Dim c() As Char = tmpStr.ToCharArray()
Dim length As Integer = c.Length - 1

For i = 0 To length
If c(i) = " " Then
c(i)
= CChar(" ")
ElseIf AscW(c(i)) > 65280 And AscW(c(i)) < 65375 Then
c(i)
= ChrW(AscW(c(i)) - 65248)
End If
Next
Return CStr(c)
End Function
End Class

 

2.关闭输入法

Me.comUser.ImeMode = Forms.ImeMode.Close ‘直接关闭全角输入

 用户登录UI 

https://files.cnblogs.com/rush/登录检验程序.rar

 

The experience of project

 

Visual Basic 不会在 Char 类型和数值类型之间直接转换。可以使用 AscAscW 函数将 Char 值转换为表示其码位的 Integer。可以使用 ChrChrW 函数将 Integer 值转换为具有该码位的 Char。

本文基于 署名 2.5 中国大陆 许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名 JK_Rush (包含链接),具体操作方式可 参考此处 。如您有任何疑问或者授权方面的协商,请 给我留言

posted @ 2010-11-07 18:34  JK_Rush  阅读(6608)  评论(1编辑  收藏  举报