0

我使用以下代码访问了 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").Rows(0).Item(1)" 行中,它给了我一个例外,说应该创建一个对象的实例。我不明白到底是什么问题。如何创建实例,我应该创建什么实例?

4

1 回答 1

0

你用了一个空格

da.Fill(ds, "Address Book") 

但是里面没有空间

txtFirstName.Text = ds.Tables("AddressBook").Rows(0).Item(1)

对于表的名称。因此,不存在名为“AddressBook”的表。

于 2012-07-08T15:28:54.737 回答