通过vb.net从mysql获取结果

时间:2019-06-20 10:59:58

标签: mysql vb.net

我正在开发一种工具来帮助公司进行员工假期(员工假期计算器)

我通过vb将自己连接起来,并将其连接到数据库,但没有任何结果

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim conn As New api()
        Dim adapter As New MySqlDataAdapter()
        Dim table As New DataTable()
        Dim command As New MySqlCommand("SELECT `Full_Name`, `Job`, `Free_Days` FROM `Holiday` WHERE `Username`= '@name'", conn.getConnection())

        command.Parameters.Add("@name", MySqlDbType.VarChar).Value = api.id

        Try
            conn.getConnection.Open()
            adapter.SelectCommand = command
            adapter.Fill(table)

            Dim sqlReader As MySqlDataReader = command.ExecuteReader()
            While sqlReader.Read()
                namee = sqlReader("Full_Name").ToString()
                job = sqlReader("Job").ToString()
                days = sqlReader("Free_Days").ToString()
                MsgBox(sqlReader("Full_Name").ToString())
            End While

            Label2.Text = "Name : " + namee
            Label3.Text = "Job : " + job
            Label8.Text = "Holiday Free Days : " & days
        Catch ex As MySql.Data.MySqlClient.MySqlException
            MsgBox(ex.Message)
        End Try
    End Sub

我没有收到任何MsgBox,也没有错误,标签文本也没有更改

1 个答案:

答案 0 :(得分:1)

我没有MySQL,但是基于MSSQL,请尝试以下操作:

Dim namee, job, days As String
Dim commandText As String = "SELECT `Full_Name`, `Job`, `Free_Days` FROM `Holiday` WHERE `Username`= '@name'"
Dim conn As New api()
Using adapter As New MySqlDataAdapter(commandText, conn.getConnection())
     adapter.SelectCommand.Parameters.Add("@name", MySqlDbType.VarChar).Value = api.id

     Dim table As New DataTable()
     adapter.Fill(table)
     If table.Rows.Count = 0 Then
        MessageBox.Show("No rows found", "ERROR")
     Else
        With table(0)
            namee = .Item("Full_Name")
            job = .Item("Job")
            days = .Item("Free_Days")
         End With
      End If
 End Using