【问题标题】:How to store user's information for future use from MySQL如何从 MySQL 存储用户信息以备将来使用
【发布时间】: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 注入攻击。请使用参数化查询。
  • 在你的项目中添加一个新类,添加一些静态属性来存储你需要的信息;如果登录有效,则使用帐户详细信息设置属性。无需创建类的实例,直接分配属性即可。然后,您可以从项目中的任何其他类中读取该类的属性。

标签: mysql .net vb.net


【解决方案1】:

Using...End Using 块确保即使出现错误,您的数据库对象也已关闭和处置。

当然,在真正的应用程序中,您永远不会将密码存储为纯文本。

行内评论。

'Your class might look something like this
Public Class User
    Public Shared ID As Integer
    Public Shared Name As String
    Public Shared Department As String
    Public Shared Address As String
End Class

Private count As Integer
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    count = count + 1
    'keep connections local for better control
    'pass the connection strings directly to the constructor of the connection
    Using cn As New MySqlConnection("Your connection string")
        'pass the query and the connection directly to the constructor of the commmand
        Using cmd As New MySqlCommand("SELECT * FROM user WHERE user.username = @User and user.password = @Password;", cn)
            'Always use parameters to avoid SQL injection
            cmd.Parameters.Add("@User", MySqlDbType.VarChar).Value = txtUsername.Text
            cmd.Parameters.Add("@Password", MySqlDbType.VarChar).Value = txtPassword.Text
            'Open the Connection at the last possible minute.
            cn.Open()
            Using dr1 = cmd.ExecuteReader
                If dr1.HasRows Then
                    dr1.Read()
                    'The indexes of the data reader depent on th order of the fields in the database
                    User.ID = CInt(dr1(0))
                    User.Name = dr1(1).ToString
                    User.Department = dr1(2).ToString
                    User.Address = dr1(3).ToString
                    Me.Hide()
                    frmDashboard.Show()
                    Return 'a successful login will end here
                End If
            End Using 'closes and disposed the reader
        End Using 'close and disposes the command
    End Using 'closes and dipose the connection
    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)
        btnLogin.Enabled = False 'Instead of the text boxes disable the button.
        'If you just disable the text boxes they can keep clicking the button and opening connections.
    End If
End Sub

【讨论】:

    猜你喜欢
    • 2014-05-01
    • 1970-01-01
    • 2015-10-28
    • 2020-07-11
    • 2013-01-05
    • 2020-06-20
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    相关资源
    最近更新 更多