你调用的对象是空的

时间:2015-11-06 05:28:24

标签: mysql vb.net

 Private Sub btnSelectAsset_Click(sender As Object, e As EventArgs) Handles btnSelectAsset.Click
    MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString =
    "server=localhost;userid=root;password= ;database=myma"
    Dim SDA As MySqlDataAdapter
    Dim dbDataSet As New DataTable
    Dim bSource As New BindingSource

    Try
        MySqlConn.Open()
        COMMAND.Connection = MySqlConn
        SDA = New MySqlDataAdapter("select * from myma.user", MySqlConn)
        dbDataSet = New DataTable
        SDA.Fill(dbDataSet)
        DataGridView1.DataSource = dbDataSet
        MySqlConn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()
    End Try
End Sub
  

对象引用未设置为对象的实例。

调试时出现上述错误。你能帮我纠正一下吗?

1 个答案:

答案 0 :(得分:0)

试试这个工作示例:

Private Sub btnSelectAsset_Click(sender As Object, e As EventArgs) Handles btnSelectAsset.Click
    Dim dbConn = New MySqlConnection
    Dim dbDataTable = New DataTable

    dbConn.ConnectionString = "server=localhost;userid=root;password= ;database=myma"

    Try
        dbConn.Open()
        If (dbConn.State = ConnectionState.Open) Then
            Dim dbAdapter = New MySqlDataAdapter("select * from myma.user", dbConn)
            dbAdapter.Fill(dbDataTable)
            DataGridView1.DataSource = dbDataTable
            dbConn.Close()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        dbConn.Dispose()
    End Try
End Sub

VB2010Ex& .NET 4客户端配置文件。