2014-08-29 26 views
1

有人可以帮我解决以下问题。使用MySQL数据库的VB.NET登录表格

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

我使用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

+0

为什么你不把你的按钮代码放在这篇文章中,而不是提供一个链接到一些下载网站? – Mych 2014-08-29 14:25:49

+0

添加了按钮代码。我也必须提供数据库设置,这就是为什么我附上截图。 – user3765415 2014-08-29 14:35:08

+0

好的......现在你总是得到什么信息?顺便说一句,你看到的东西存储密码未加密的是你的意图吗? – Mych 2014-08-29 14:40:14

回答

0

你有一些额外的空格线

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(),以使其更清晰,更少的机会忽略那些多余的空格。

+0

干杯。你能告诉我哪里留下了额外的空间吗?如果我删除多余的空格,那么在我的格式上使用你的格式会有什么不利吗?我只想知道我的编码是否更糟。 :) – user3765415 2014-08-29 15:23:30

+0

...'和'之间的用户名='“',同样在密码上。 – djv 2014-08-29 15:31:50

+0

'username ='{0}''中的额外空间与'where username ='“'相比是非常明显的。在这种简单的情况下,这是一个优先选择的问题,尽管我认为清晰度比使用String.Format在功能上,它们是一样的 – djv 2014-08-29 15:34:35