본문 바로가기
MySql

MySQL 데이터베이스를 사용하는 VB.NET 로그인 양식

by 베이스 공부 2020. 12. 1.
반응형

누군가가 다음과 같이 나를 도울 수 있습니까?

dbhr라는 데이터베이스와 VARCHAR 데이터 유형을 가진 'username'과 'password'필드가있는 user라는 테이블을 만들었습니다. 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. 도와주세요.

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

데이터베이스 테이블 레코드 및 데이터 유형을 보려면 다음 링크를 클릭하십시오.


 

해결 방법

 

줄에 여분의 공백이 있습니다.

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 () 을 사용하여 추가 공간을 간과 할 가능성을 줄이고 명확하게 만듭니다.

 

참조 페이지 https://stackoverflow.com/questions/25570057

 

 

반응형

댓글