【发布时间】:2019-04-19 20:45:08
【问题描述】:
我有一个非常简单的应用程序,用户将在其中登录并被重定向到仪表板。我让登录部分正常工作,但是,我的下一个目标是能够存储用户信息以供以后在其他表单上使用。
示例:用户“admin”成功登录。我需要能够为 admin 存储表中的每一列,以便我们可以调用用户的欢迎信息、用户信息表单等信息,而不必每次都查询数据库。 p>
我相信这可以通过一个类来完成,但是,我不确定如何重写我的登录脚本以将所有详细信息保存到一个类中。
我尝试创建一个类,并为每一列添加Public Shared 属性,但我不确定如何将每一列都放入类中,而不仅仅是用户名。
Imports MySql.Data.MySqlClient
Public Class frmLogin
'count is number of invalid login attempts
Dim count As Integer
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
count = count + 1
Dim x As New MySqlConnection
Dim admin As New MySqlCommand
Dim dr1 As MySqlDataReader
ConnectDatabase()
admin.Connection = conn
admin.CommandText = "SELECT user.username, user.password FROM user WHERE user.username = '" & txtUsername.Text & "' and user.password = '" & txtPassword.Text & "'"
dr1 = admin.ExecuteReader
If dr1.HasRows Then
'Read the data
dr1.Read()
Me.Hide()
frmDashboard.Show()
Else
MsgBox("Invalid Username or Password! " & vbCrLf & count & " out of 3 attempts remaining.")
If count >= 3 Then
MsgBox("You have exceeded the maximum number of attempts to login. Account has been disabled. Please contact OJFS helpdesk at extension 100.", MsgBoxStyle.Critical)
txtUsername.Enabled = False
txtPassword.Enabled = False
End If
End If
Connect.conn.Close()
End Sub
Dim Assistance As Boolean = False
Private Sub linkLoginHelp_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles linkLoginHelp.LinkClicked
If Assistance = True Then
Me.Height = 284
Me.CenterToScreen()
Assistance = False
txtUsername.Select()
Else
Me.Height = 463
Me.CenterToScreen()
Assistance = True
txtUsername.Select()
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
End Class
【问题讨论】:
-
您的应用程序容易受到 SQL 注入攻击。请使用参数化查询。
-
在你的项目中添加一个新类,添加一些静态属性来存储你需要的信息;如果登录有效,则使用帐户详细信息设置属性。无需创建类的实例,直接分配属性即可。然后,您可以从项目中的任何其他类中读取该类的属性。