用戶登錄驗證程序——VB.NET
Introduction
實現用戶登錄功能,UI使用WinForm提供LoginForm,然后建一個User表保存用戶信息(UserName, PassWrd, UserActive, Comment),使用Linq對User表進行查詢。
Backgroud
使用Visual studio2008,sql server 2005開發
The Definition of User Table
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 ‘直接關閉全角輸入
https://files.cnblogs.com/rush/登錄檢驗程序.rar
The experience of project
Visual Basic 不會在 Char 類型和數值類型之間直接轉換。可以使用 Asc 或 AscW 函數將 Char 值轉換為表示其碼位的 Integer。可以使用 Chr 或 ChrW 函數將 Integer 值轉換為具有該碼位的 Char。
本文基于 署名 2.5 中國大陸 許可協議發布,歡迎轉載,演繹或用于商業目的,但是必須保留本文的署名 JK_Rush (包含鏈接),具體操作方式可 參考此處 。如您有任何疑問或者授權方面的協商,請 給我留言 。
|
|
關于作者:[作者]:
JK_Rush從事.NET開發和熱衷于開源高性能系統設計,通過博文交流和分享經驗,歡迎轉載,請保留原文地址,謝謝。 |

用戶登錄驗證程序,提供一個Login UI使用ADO.NET把數據保存到DB中
浙公網安備 33010602011771號