2014-08-29 2 views
1

다음 사람이 나를 도와 줄 수 있습니까?MySQL 데이터베이스를 사용하는 VB.NET 로그인

dbhr이라는 데이터베이스를 만들고이 테이블에 VARCHAR 데이터 형식을 갖는 'username'과 'password'라는 두 개의 필드가있는 user라는 사용자가 있습니다. 두 개의 텍스트 상자 (tbxUsername, tbxPassword)와 확인 버튼이있는 로그인 양식이 있습니다. 데이터베이스를 연결하여 사용자 이름과 암호를 인증했습니다. 그러나 그것은 항상 나에게 틀린 암호 메시지를 준다. 내가 어디가 잘못되었는지 모르겠다. 도와주세요.

나는 사전에 MySQL의 워크 벤치 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

+0

버튼 코드가 ​​추가되었습니다. 나는 또한 스크린 샷을 첨부하는 이유는 db 설정을 제공했다. – user3765415

+0

OK ... 이제 어떤 메시지를 항상 받으시겠습니까? BTW ... 암호화되지 않은 비밀 번호를 저장하고있는 것들의 외모에 의해 이것은 당신의 의도입니까? – Mych

답변

0

당신은 내가 그것을 명확하게, 적은 기회는 그 여분의 공간을 간과 할 수 있도록 String.Format()을 사용

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

로 변경 할 줄

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

에 몇 가지 여분의 공간이있다.

+0

건배. 여분의 공간을 어디에두고 왔는지 말해 줄 수 있어요? 여분의 공백을 제거하면 내 형식을 사용할 때의 단점이 있습니까? 나는 그저 코드 작성이 더 나쁜지 알고 싶을 뿐이다. :) – user3765415

+0

...'여기서 'username'= '''와 '와'는 비밀번호와 같습니다. – djv

+0

'username = '{0}'의 여분의 공간은'where username = ' "'과 비교할 때 매우 분명합니다. 이처럼 간단한 경우 선호도의 문제입니다. – djv

관련 문제