gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:57:38 26 4
gpt4 key购买 nike

请有人帮我解决以下问题。

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

我使用 MySQL Workbench 6.1

提前致谢。

这是 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

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

点击here !

最佳答案

行中有一些额外的空格

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

我会改成

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

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

关于mysql - 使用 MySQL 数据库的 VB.NET 登录表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25570057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com