如何使用www.example.com读取和比较MySQL数据库中的数据值vb.net?

vaj7vani  于 2023-05-16  发布在  Mysql
关注(0)|答案(1)|浏览(112)

我试图从用户那里收集输入,根据我的数据库检查它,看看它是否存在于数据库中。如果是这样的话,程序应该获取相应个人的姓、名和费用并输出。
验证码:

Imports Mysql.Data.MySqlClient

Public Class Form1
    Dim reader As MySqlDataReader
    Dim command As New MySqlCommand

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim myConnectionString As String

        myConnectionString = "server=localhost;" _
              & "uid=root;" _
              & "pwd=Emma@21*GCTU;" _
              & "database=dummystudents"

        Try
            Dim conn As New MySql.Data.MySqlClient.MySqlConnection(myConnectionString)
            conn.Open()
            Dim sql As String = "SELECT idstudents FROM students WHERE idstudents = @TextBox1.Text "
            command = New MySqlCommand(sql, conn)
            reader = command.ExecuteReader()

            If reader.Read() Then
                TextBox2.Text = reader(1)
                TextBox3.Text = reader(2)
                TextBox4.Text = reader(3)
            End If

        Catch ex As MySql.Data.MySqlClient.MySqlException
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
wrrgggsh

wrrgggsh1#

下面是你的SQL代码:

SELECT idstudents FROM students WHERE idstudents = @TextBox1.Text

idstudents是你输入的值时,把idstudents拉出来有什么意义?更糟糕的是,这就是你所做的一切,然后你这样做:

TextBox2.Text = reader(1)
TextBox3.Text = reader(2)
TextBox4.Text = reader(3)

这需要你至少撤回四列。
注解中提到的修改可能会让您的代码执行,但这不是正确的方法。看起来您尝试使用参数但失败了。这样做,但要做对,即。

Dim sql As String = "SELECT idstudents, otherColumnsHere FROM students WHERE idstudents = @idstudents"

Using connection As New MySqlConnection(myConnectionString),
      command As New MySqlCommand(sql, connection)
    command.Parameters.Add("@idstudents", MySqlDbType.Int32).Value = CInt(TextBox1.Text)
    conn.Open()

    Using reader = command.ExecuteReader()
        If reader.Read() Then
            TextBox2.Text = reader(1)
            TextBox3.Text = reader(2)
            TextBox4.Text = reader(3)
        End If
    End Using
End Using

相关问题