首页 > 解决方案 > 如何将登录按钮与mysql数据库连接

问题描述

我的代码是一个登录表单,由于代码中的一些错误,它无法运行。我不知道如何调试它。我是编程初学者

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Cmdsignin.Click
    If Txtusername.Text = "" Then
        MessageBox.Show("Please Input the Username", "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
        Exit Sub
    End If
    If Txtpassword.Text = "" Then
        MessageBox.Show("Please Input the   password", "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
        Exit Sub
    End If
    Try
        Dim DataRead As OdbcReader
        Dim CmDSave As New OdbcCommand
        With CmDSave
            .Connection = dbconnection
            .CommandText = "SELECT * FROM users WHERE username= '" & Txtusername.Text & "' and password= '" & Txtpassword.Text & "', conn"

            .Parameters.Add("p1", Odbc.OdbcType.VarChar, 45).Value = Txtusername.Text
            .Parameters.Add("p1", Odbc.OdbcType.VarChar, 45).Value = Txtpassword.Text
            DataRead = .ExecuteNonQuery()
        End With
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
    End Try

    If DataRead.HasRows() Then
        MessageBox.Show("Login success!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
        MDIMenu.Show()
        Me.Close()

    Else

        MsgBox("Invalid Login information.!", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If

    dbconnection.Close()

End Sub

标签: vb.net

解决方案


我想知道您使用的需要 ODBC 的数据库。我已将用户界面代码与数据库代码分开。我不确定 anOdbcReader是什么。你有这个名字的自定义类吗?

需要处理命令和连接等数据库对象。Using...End Using即使有错误也会处理。这些对象应在使用它们的方法中声明,以便可以使用 Using 块。

您可以将连接字符串传递给 Connection 构造函数,并将 and 传递CommandTextConnection命令的构造函数。

Select 查询不是 NonQuery。ExecuteNonQuery与插入、更新和删除命令一起使用。

您的参数没有用,因为它们没有出现在CommandText.

Private ConnStr As String = "Your connection string"

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If Txtusername.Text = "" Then
        MessageBox.Show("Please Input the Username", "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
        Exit Sub
    End If
    If Txtpassword.Text = "" Then
        MessageBox.Show("Please Input the   password", "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
        Exit Sub
    End If
    Dim Success As Boolean
    Try
        Success = IsLoginValid(Txtusername.Text, Txtpassword.Text)
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Data Input Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
        Exit Sub
    End Try

    If Success Then
        MessageBox.Show("Login success!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
        MDIMenu.Show()
        Me.Close()
    Else
        MessageBox.Show("Invalid Login information.!", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
End Sub

Private Function IsLoginValid(UName As String, PWord As String) As Boolean
    Dim dt As New DataTable
    Using dbconnection As New OdbcConnection(ConnStr),
            CmDSave As New OdbcCommand("SELECT * FROM users WHERE username= p1 and password= p2;", dbconnection)
        CmDSave.Parameters.Add("p1", Odbc.OdbcType.VarChar, 45).Value = UName
        CmDSave.Parameters.Add("p2", Odbc.OdbcType.VarChar, 45).Value = PWord
        dbconnection.Open()
        Using reader = CmDSave.ExecuteReader
            dt.Load(reader)
        End Using
    End Using
    If dt.Rows.Count > 0 Then
        Return True
    Else
        Return False
    End If
End Function

这可以更容易地完成,但在不知道您需要什么风格的 sql 的情况下,我单独留下了选择。


推荐阅读