简体   繁体   English

VB.NET SQL数据库登录

[英]VB.NET SQL Database Login

I have a database login working with use of a MySQL database. 我有使用MySQL数据库的数据库登录名。 The user is brought to a menu screen if their login is successful. 如果用户登录成功,则将其带到菜单屏幕。 Everything works fine but what I'm trying to do is bring the user to a separate admin screen if their "UserGroup" field in the database says "Admin" 一切正常,但我要尝试的是如果数据库中的“用户组”字段显示“管理员”,则将用户转到单独的管理屏幕

I'm stuck so far, the working code for everything is as follows: 到目前为止,我仍然受阻,所有内容的工作代码如下:

Imports MySql.Data.MySqlClient

Public Class frmLogin
    Dim MysqlConn As MySqlConnection
    Dim command As MySqlCommand
    Dim reader As MySqlDataReader

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click

        MysqlConn = New MySqlConnection
        MysqlConn.ConnectionString =
        "server=localhost;port=3307;userid=root;password=Djmmcm93;database=dojodb"

        Dim reader As MySqlDataReader ' had to be declared inside the button or would not work


        Try
            MysqlConn.Open() ' opening the connection to the DB
            Dim query As String
            query = "select * from dojodb.userinfo where UserID='" & txtUserID.Text & "' and Password='" & txtPassword.Text & "'"
            command = New MySqlCommand(query, MysqlConn)
            reader = command.ExecuteReader 'executes the command and reads data from db

            Dim count As Integer
            count = 0
            While reader.Read
                count = count + 1
            End While

            If count = 1 Then
                MessageBox.Show("Welcome!")
                Me.Hide()
                frmUserMenu.Show()
            ElseIf count > 1 Then
                MessageBox.Show("username and password are duplicated!") 'Only here as test
            Else
                MessageBox.Show("username and password are incorrect!")
            End If

            MysqlConn.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message) 'printing the exact error to help future testing if needed
        Finally
            MysqlConn.Dispose()
        End Try
    End Sub
End Class

The first thing I noticed and changed was SELECT *. 我注意到并更改的第一件事是SELECT *。 For performance reasons, you should try never to select * and specify columns you want. 出于性能原因,您应尽量不要选择*并指定所需的列。 If there are more columns, feel free to add them. 如果有更多列,请随时添加。

Then I adjusted your code, hopefully it helps you: 然后我调整了您的代码,希望它对您有所帮助:

Public Class frmLogin
    Dim MysqlConn As MySqlConnection
    Dim command As MySqlCommand
    Dim reader As MySqlDataReader

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click

        MysqlConn = New MySqlConnection
        MysqlConn.ConnectionString =
        "server=localhost;port=3307;userid=root;password=Djmmcm93;database=dojodb"

        Dim reader As MySqlDataReader ' had to be declared inside the button or would not work


        Try
            MysqlConn.Open() ' opening the connection to the DB
            Dim query As String
            query = "select UserID,Password from dojodb.userinfo where UserID='" & txtUserID.Text & "' and Password='" & txtPassword.Text & "'"
            Dim userStatus = "select UserGroup from dojodb.userinfo where UserID='" & txtUserID.Text & "' and Password='" & txtPassword.Text & "'"
            command = New MySqlCommand(query, MysqlConn)
            reader = command.ExecuteReader 'executes the command and reads data from db

            If UserGroup = "Admin" Then

                'Send to admin page here


            Else            

                Dim count As Integer
                count = 0
                While reader.Read
                count = count + 1
                End While

                If count = 1 Then
                MessageBox.Show("Welcome!")
                Me.Hide()
                frmUserMenu.Show()
                ElseIf count > 1 Then
                MessageBox.Show("username and password are duplicated!") 'Only here as test
                Else
                MessageBox.Show("username and password are incorrect!")
                End If

                MysqlConn.Close()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message) 'printing the exact error to help future testing if needed
        Finally
            MysqlConn.Dispose()
        End Try
    End Sub
End Class

You can try to modify the next portion of your code: 您可以尝试修改代码的下一部分:

Dim count As Integer
Dim UserGroup As String = ""   ' <───────  Insert this line.
count = 0
While reader.Read
    count = count + 1
    UserGroup = reader("UserGroup").ToString   ' <───────  Insert this line ("UserGroup" is the name of the field in the UserInfo table).
End While

If count = 1 Then
    MessageBox.Show("Welcome!")

    If UserGroup = "Admin" Then   ' ' <───────  Insert this If to verified UserGroup.
        ' bring the user to separate admin screen
    Else
        Me.Hide()
        frmUserMenu.Show()
    End If
ElseIf count > 1 Then
    MessageBox.Show("username and password are duplicated!") 'Only here as test
Else
    MessageBox.Show("username and password are incorrect!")
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM