簡體   English   中英

運行查詢時用戶被拒絕訪問

[英]User is denied access when running a query

我的 vb.net 代碼應該連接到我的數據庫。 它成功地做到了這一點,直到我添加了一個查詢來檢查用戶名和密碼是否存在。 添加后,用戶被拒絕訪問,同時仍然使用相同的密碼。 這可能是什么原因? 代碼:

MysqlConn = New MySqlConnection()

    MysqlConn.ConnectionString = "server=;" _
    & "user id=;" _
    & "password=;" _
    & "database="

    Try
        MysqlConn.Open()
        Using cmd As New MySqlCommand
            cmd.CommandText = "SELECT COUNT(*) From tableUser WHERE Username=" & TextBox1.Text & " AND Password=" & TextBox2.Text
            cmd.CommandType = CommandType.Text
            cmd.Connection = MysqlConn
            result = cmd.ExecuteScalar
        End Using
        MysqlConn.Close()
        If (result < 1) Then
            MessageBox.Show("Please make sure you have typed valid credentials!")
        ElseIf result = 1 Then
            Dim form As New Form2
            form.Show()
            Me.Close()
        End If
    Catch myerror As MySqlException
        MessageBox.Show("Connection to the database has been lost. Please try again later.")
    Finally
        MysqlConn.Dispose()
    End Try

這是一些代碼,我剛剛寫了它,希望它有所幫助,只需根據您的需要進行更改。

Private Sub login()
    conn = New MySqlConnection
    con.ConnectionString =
        "server=localhost;userid=root;password=1234;database=batabase;port=3307"
    Dim READER As MySqlDataReader
    If TextBox1.Text = "" Then


        MessageBox.Show("Please enter usename ! ", "Enter username")
        Exit Sub

    End If
    If TextBox2.Text = "" Then

        MessageBox.Show("Please enter password", "Enter password")
        Exit Sub
    End If


    Try


        conn.Open()
        Dim Query As String
        Query = "select * from db.users where user='" & TextBox1.Text & "' and password='" & TextBox2.Text & "' "
        COMMAND = New MySqlCommand(Query, conn)
        READER = COMMAND.ExecuteReader
        Dim count As Integer

        count = 0
        While READER.Read
            count = count + 1
        End While

        If count = 1 Then

            Form.Show()
            Me.Hide()

        ElseIf count > 1 Then
            MessageBox.Show("The username is already in use!")
        Else
            MsgBox("Error , please try again ", MsgBoxStyle.Critical)
        End If




        conn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        conn.Dispose()
    End Try
End Sub

將數據庫名稱用戶名更改為您的需要,它將起作用!

只需在表單上的登錄按鈕中添加 login() 即可!!! 或在沒有 Private Sub login() 的情況下在您的按鈕上輸入此代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM