2015-11-06 70 views
0
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 

對象引用不設置爲一個對象的實例的對象的一個​​實例。對象引用不設置到

調試時出現上述錯誤。你能幫我解決這個問題嗎?

+0

你哪一行出錯? –

+0

沒有指定行 – luckydzs

+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客戶端配置文件。

相關問題