简体   繁体   English

位置 0 Vb.net 和 Mysql 没有行

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

I'm trying to get and print data from a row "status" on the table "printer" and it keeps saying "There is no row at position 0."我试图从表“打印机”上的“状态”行获取和打印数据,但它一直说“位置 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")

The connection:连接:

 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

Open connection:打开连接:

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

What is wrong?怎么了?

You haven't executed the command to fill the table.您尚未执行填充表的命令。 Without that part the table is still empty没有那部分,桌子仍然是空的

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")

Even after this the table could still be empty if there is no record in the database table named Printer, so before reading anything from a datatable check always the rows count即使在此之后,如果名为 Printer 的数据库表中没有记录,该表仍可能为空,因此在从数据表中读取任何内容之前,请始终检查行数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM