我使用以下代码访问了VB.NET中的数据库:
Public Class Form1
Private Sub btnLoad_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLoad.Click
Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = AddressBook.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Address Book")
MsgBox("Database Open")
con.Close()
MsgBox("Database Closed")
txtFirstName.Text = ds.Tables("AddressBook").Rows(0).Item(1)
txtSurname.Text = ds.Tables("AddressBook").Rows(0).Item(2)
End Sub
End Class
但是在行“txtFirstName.Text = ds.Tables(”AddressBook“)。行(0).Item(1)”,它给了我一个例外,说明应该创建一个对象的实例。我不明白究竟是什么问题。我如何创建一个实例,以及我应该创建一个实例?
答案 0 :(得分:0)
您在
中使用了空格da.Fill(ds, "Address Book")
但
中没有空格txtFirstName.Text = ds.Tables("AddressBook").Rows(0).Item(1)
表格的名称。因此,不存在名为“AddressBook”的表。