mysql - 使用 Visual Basic 通过 mySql 数据库验证 StudentId 和 StudentPassword

标签 mysql vb.net

我正在开发学校投票系统。我已经尝试了几次并且没有错误,但是如果我输入详细信息并单击登录,我的登录按钮将不起作用。 我使用 Visual Studio 2013,如果有人能提供帮助,我将很高兴。 谢谢

Imports MySql.Data.MySqlClient

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ConnectToSQL()
End Sub

'connecting to sql method
Private Sub ConnectToSQL()
    Dim con As New MySqlConnection
    Dim cmd As New MySqlCommand
    Dim StudentId As String
    Dim StudentPassword As String

    Try

        If con.ConnectionString = "Data source= localhost; port=3306; database= Students; user=root; password=;" Then
            con.Open()

            cmd.Connection = con
            cmd.CommandText = "SELECT StudentId, StudentPassword, StudentName FROM members"

            Dim lrd As MySqlDataReader = cmd.ExecuteReader()
            If lrd.HasRows Then
                While lrd.Read()
                    StudentId = lrd("StudentId").ToString
                    StudentPassword = lrd("StudentPassword").ToString
                    If StudentPassword = TextBox1.Text And StudentId = TextBox1.Text Then
                        MsgBox("you logged in succesfully")
                        Me.Hide()
                        Form2.Show()

                        TextBox1.Text = ""
                        TextBox2.Text = ""
                    End If

                End While

            Else
                MsgBox("Username and password do not match")
                TextBox2.Text = ""
            End If
        End If

    Catch ex As Exception
        MsgBox(ex.Message)

    Finally
        con.Close()
    End Try
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox1.Focus()
End Sub
End Class

最佳答案

据我所知,您的问题是您的第一个 if 语句:

    If con.ConnectionString = "Data source= localhost; port=3306; database= Students; user=root; password=;" Then

这会检查连接字符串是否设置为该值,并且肯定会评估为 false。

我假设您想要设置这些设置,然后打开与数据库的连接,而不检查这些设置是否设置为特定字符串

要使其工作,只需删除此 if 语句并像这样设置连接字符串,然后打开连接

    con.ConnectionString = "Data source= localhost; port=3306; database= Students; user=root; password=;" Then

关于mysql - 使用 Visual Basic 通过 mySql 数据库验证 StudentId 和 StudentPassword,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48578895/

相关文章:

vb.net - 非阻塞网络请求 vb.net

asp.net-mvc - Azure 应用服务 Web 应用编译错误

php - 数据库编码问题?显示带问号的双引号和单引号

php - 更新了查询,显示 PDO 中的旧数据

json - 在 Visual Basic 中反序列化 JSON

VB.NET 迭代表单标签

MySQL\regexp\仅搜索包含特定字符的记录

php - 根据每个 id 的自定义公式,仅使用 1 个输入字段更改 MySQL 价格?

mysql - 我需要在 MySQL 语句中做出 promise 吗?

c# - 从解决方案中的其他项目访问 MySettings