使用 MySQL 数据库的 VB.NET 登录表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25570057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:07:52  来源:igfitidea点击:

VB.NET log in form using MySQL database

mysqlvb.net

提问by user3765415

Can someone help me with the following please.

有人可以帮我解决以下问题吗?

I have created a database called dbhr and a table in it called user with two fields 'username' and 'password' having VARCHAR data type. I have a log in form with two textboxes (tbxUsername,tbxPassword) and an OK button. I have connected my database to authenticate the username and password. But it always give me wrong password message. I don't know where I went wrong. Please help.

我创建了一个名为 dbhr 的数据库和一个名为 user 的表,其中包含两个具有 VARCHAR 数据类型的字段“用户名”和“密码”。我有一个登录表单,其中包含两个文本框(tbxUsername、tbxPassword)和一个 OK 按钮。我已经连接了我的数据库来验证用户名和密码。但它总是给我错误的密码信息。我不知道我哪里错了。请帮忙。

I use MySQL Workbench 6.1

我使用 MySQL Workbench 6.1

Thanks in advance.

提前致谢。

Here is the VB.NET log in button code.

这是 VB.NET 登录按钮代码。

Imports MySql.Data.MySqlClient

Public Class Login

    Dim mydbcon As MySqlConnection
    Dim COMMAND As MySqlCommand

    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        mydbcon = New MySqlConnection
        mydbcon.ConnectionString = "server=localhost;userid=root;password=rootword;database=hrdb"
        Dim reader As MySqlDataReader

        Try
            mydbcon.Open()
            Dim Query As String
            Query = "select * from user where username= ' " & tbxUsername.Text & "' and password= ' " & tbxPassword.Text & "' "
            COMMAND = New MySqlCommand(Query, mydbcon)
            reader = COMMAND.ExecuteReader

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

            End While

            If count = 1 Then
                MessageBox.Show("Username and password are correct")
            ElseIf count > 1 Then
                MessageBox.Show("Username and password are duplicate")
            Else
                MessageBox.Show("Username and password are wrong")
            End If
            mydbcon.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            mydbcon.Dispose()
        End Try

    End Sub

Please click the following links to see the database table records and data types.

请点击以下链接查看数据库表记录和数据类型。

Click here!

点击这里

采纳答案by djv

You have some extra spaces in the line

您在该行中有一些额外的空格

Query = "select * from user where username= ' " & tbxUsername.Text & "' and password= ' " & tbxPassword.Text & "' "

which I would change to

我会改为

Query = String.Format("SELECT * FROM user WHERE username = '{0}' AND password = '{1}'", Me.tbxUsername.Text.Trim(), Me.tbxPassword.Text.Trim())

I would use String.Format()to make it clearer, less chance to overlook those extra spaces.

我会String.Format()用来使它更清晰,减少忽略那些额外空间的机会。