我有这段代码,我在 Visual Studio 2012 中使用它来从 sql 数据库中的数据填充组合框。每次我尝试调试它时,我都会在空引用异常的 for 循环中得到异常。我希望有人可以帮助我弄清楚我在这里缺少什么。
Private Sub StationList_Click(sender As Object, e As EventArgs) 处理 StationList.Click
Dim cn As New OleDbConnection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\hendera2\Desktop\StationEquip.accdb"
Dim Mystr As String = "SELECT DISTINCT Location FROM StationEquipment"
Dim Myadapter As OleDbDataAdapter
Dim Mydataset As New DataSet()
Try
cn.Open()
Myadapter = New OleDbDataAdapter(Mystr, cn.ConnectionString)
Myadapter.Fill(Mydataset, Mystr)
Dim dr As DataRow
For Each dr In Mydataset.Tables("StationEquipDataSet").Rows
Me.StationList.Items.Add(dr("Location"))
Next
cn.Close()
Catch ex As Exception
MsgBox("Cannot Open connection")
End Try
End Sub