在位置 0 Vb.net 和 Mysql 处没有行

There is no row at position 0 Vb.net and Mysql

我正在尝试从 table“打印机”上的“状态”行获取并打印数据,但它一直显示“位置 0 处没有行”。

 Dim conn As New deepconnection()
    Dim adapter As New MySqlDataAdapter()
    Dim table As New DataTable()
    Dim ds, ds1 As New DataSet
    Dim command As New MySqlCommand("SELECT * FROM printer", conn.getConnection)
    conn.openOcean()
    PrinterStatus.Text = table.Rows(0).Item("status")

连接:

 Private fishcatch As New MySqlConnection("datasource=localhost;port=3306;username=root;password=xxxxx;database=deep_ocean")

' Get the connection only to read
ReadOnly Property getConnection() As MySqlConnection
    Get
        Return fishcatch
    End Get
End Property

打开连接:

Sub openOcean()
    If fishcatch.State = ConnectionState.Closed Then
        fishcatch.Open()
    End If
End Sub

怎么了?

您还没有执行填充table的命令。没有那部分 table 仍然是空的

Dim command As New MySqlCommand("SELECT * FROM printer", conn.getConnection)
conn.openOcean()

' Execute the command and pass the reader to the table load method
table.Load(command.ExecuteReader())

PrinterStatus.Text = table.Rows(0).Item("status")

即使在这之后,如果数据库 table 中没有名为 Printer 的记录,table 仍然可能为空,因此在从数据 table 读取任何内容之前,请始终检查行计数

If table.Rows.Count > 0 Then
    PrinterStatus.Text = table.Rows(0).Item("status")
    ...
End If