数据库和显示结果

时间:2019-05-10 07:42:15

标签: vb.net visual-studio-2017 mysql-workbench

我是编码新手。看来我无法在这一结果中得出结果。我正在创建一个RFID Tap Card系统。

当我点击存储卡(数据库中有信息)时,数据将被保存(将出现PictureBox1)。但是,当我尝试使用数据库中还没有的另一张卡时,代码不会采用“ Else”语法,或者我应该说PictureBox2没有显示,仍然出现了Picturebox1。这可能是另一种方式吗?谢谢。这是代码。

    Private Sub txtReceived_TextChanged(sender As Object, e As EventArgs) Handles txtReceived.TextChanged

    Dim mysql = "SELECT Schedule FROM tbl_patroninfo WHERE Schedule = @Sched"
    Dim myDate As Date = lbl_Date.Text
    myDate = Date.Today
    con = connectDB()
    con.Open()
    mycommand = New MySqlCommand(mysql, con)
    Dim dr As MySqlDataReader = mycommand.ExecuteReader
    Dim SelectCommand As New MySqlClient.MySqlCommand("Select Fullname From tbl_patroninfo where PatronRFID = @PID", con)
    SelectCommand.Parameters.Add("@PID", MySqlDbType.VarChar).Value = txtReceived.Text
    Dim SelectCommand2 As New MySqlClient.MySqlCommand("select Department from tbl_patroninfo where PatronRFID = @PID", con)
    SelectCommand2.Parameters.Add("@PID", MySqlDbType.VarChar).Value = txtReceived.Text

    If dr.Read = False Then
        con.Close()
        con.Open()
        lbl_Full.Text = CStr(SelectCommand.ExecuteScalar())
        lbl_Department.Text = CStr(SelectCommand2.ExecuteScalar())
        mycommand = New MySqlCommand("update tbl_patroninfo set Schedule = @Sched WHERE PatronRFID = @PID", con)
        mycommand.Parameters.Add("@Sched", MySqlDbType.Date).Value = myDate
        mycommand.Parameters.Add("@PID", MySqlDbType.VarChar).Value = txtReceived.Text
        mycommand.ExecuteNonQuery()
        PictureBox1.Visible = True
        con.Close()
    Else
        con.Close()
        con.Open()
        PictureBox2.Visible = True
        lbl_Full.Text = "RECORD NOT FOUND!"
        con.Close()
    End If
End Sub

2 个答案:

答案 0 :(得分:1)

感谢您对@Daffas和@Charles May的帮助。我只是做了一些不太酷的事情,或者我应该说“欺骗代码”来使系统运行。这仅用于项目目的。但是在系统的下一版本中,我将使代码正确。我只是使用了一些标签和捕捉器文本框,并将其设置为“隐藏”模式。而不是使用dr.HasRows(这对我来说效果不佳),我使用了executeScalar编码并实现了这一点。到目前为止,这是我的代码:

    Dim SelectCommand As New MySqlClient.MySqlCommand("Select Fullname From tbl_patroninfo where PatronRFID = @PID", con)
    SelectCommand.Parameters.Add("@PID", MySqlDbType.VarChar).Value = txtReceived.Text
    Dim SelectCommand2 As New MySqlClient.MySqlCommand("select Department from tbl_patroninfo where PatronRFID = @PID", con)
    SelectCommand2.Parameters.Add("@PID", MySqlDbType.VarChar).Value = txtReceived.Text
    lbl_Full.Text = txt_Catch.Text
    lbl_Full.Text = CStr(SelectCommand.ExecuteScalar())
    lbl_Department.Text = CStr(SelectCommand2.ExecuteScalar())

    If txt_Catch.Text = lbl_Full.Text Then
        con.Close()
        con.Open()
        lbl_Full.Text = "RECORD NOT FOUND!"
        PictureBox2.Visible = True
        con.Close()
        txt_Catch.Clear()

    Else
        con.Close()
        con.Open()
        '  lbl_Full.Text = CStr(SelectCommand.ExecuteScalar())
        ' lbl_Department.Text = CStr(SelectCommand2.ExecuteScalar())
        mycommand = New MySqlCommand("update tbl_patroninfo set Schedule = @Sched WHERE PatronRFID = @PID", con)
        mycommand.Parameters.Add("@Sched", MySqlDbType.Date).Value = myDate
        mycommand.Parameters.Add("@PID", MySqlDbType.VarChar).Value = txtReceived.Text
        mycommand.ExecuteNonQuery()
        DataGridView1.Rows.Add(lbl_Full.Text, lbl_Time.Text)
        PictureBox1.Visible = True
        con.Close()

    End If

答案 1 :(得分:0)

您可能想尝试使用dr.HasRows()代替dr.Read,如果没有任何内容,它将返回false。 dr.Read通常在While循环中使用:

While dr.Read
   ' Do stuff on the records here.
End While