简体   繁体   English

运行查询时用户被拒绝访问

[英]User is denied access when running a query

my vb.net code is supposed to connect to my database.我的 vb.net 代码应该连接到我的数据库。 It did this successfully until I added a query to check if the username and password existed.它成功地做到了这一点,直到我添加了一个查询来检查用户名和密码是否存在。 After adding this, the user is denied access whilst still using the same password.添加后,用户被拒绝访问,同时仍然使用相同的密码。 What could be the cause of this?这可能是什么原因? Code:代码:

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

Here is some code , i just wrote it , hope it helps ,just change it to your needs.这是一些代码,我刚刚写了它,希望它有所帮助,只需根据您的需要进行更改。

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

Change the database name user name to your needs and it will work !将数据库名称用户名更改为您的需要,它将起作用!

just add the login() in your login button on your form !!!只需在表单上的登录按钮中添加 login() 即可!!! or enter this code on your button without the Private Sub login()或在没有 Private Sub login() 的情况下在您的按钮上输入此代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM