VB.NET显示从MySql到Datagrid的数据

时间:2018-04-16 18:38:30

标签: mysql vb.net

我已经阅读了之前帖子中的所有问题和答案 但不适合我的问题

问题: 我想显示从db(mysql)到datagrid的数据 但问题是MySqlDataReader无法识别

SelectCommand

Fill 

Update 

作为MySqlDataReader

的成员

我遵循与教程中代码编写完全相同的方式 为什么会出现这个问题?

下面我附上了我的代码。 我希望有人可以帮助我。

Imports MySql.Data.MySqlClient

Public Class AdminAddItem
Dim MysqlConnection As MySqlConnection
Dim FoodCommand As MySqlCommand
Dim DataFoodRead As MySqlDataReader
Dim dbDataSetFood As New DataTable

Private Sub btnLoadFood_Click(sender As Object, e As EventArgs) Handles btnLoadFood.Click
    'declare new connection
    MysqlConnection = New MySqlConnection

    'prepare connection string
    MysqlConnection.ConnectionString =
        "server = localhost; userid = root; password = 1234 ; database = kedaikopimamba_db"

    Dim bSource As New BindingSource

    Try
        MysqlConnection.Open()

        Dim query As String
        query = "select * from kedaikopimamba_db.foodtable"

        FoodCommand.Connection = MysqlConnection
        FoodCommand.CommandText = query

       DataFoodRead.SelectCommand = FoodCommand
        DataFoodRead.Fill(dbDataSetFood)
        bSource.DataSource = dbDataSetFood
        dgDisplayItem.DataSource = bSource
        DataFoodRead.Update(dbDataSetFood)


        MysqlConnection.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        MysqlConnection.Dispose()
    End Try

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您的代码的哪个部分是错误?请让我们知道以便轻松识别问题。