简体   繁体   English

VB.NET中的数据库访问

[英]Database Access in VB.NET

I have accessed a database in VB.NET using the following code: 我已经使用以下代码访问了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  

However in line "txtFirstName.Text = ds.Tables("AddressBook").Rows(0).Item(1)", it gives me an exception saying that an instance of an object should be created. 但是,在“ txtFirstName.Text = ds.Tables(“ AddressBook”)。Rows(0).Item(1)”行中,它给了我一个例外,该例外指出应创建对象的实例。 I am not understanding what exactly the problem is. 我不明白到底是什么问题。 How can I create an instance, and of WHAT should I create an instance? 如何创建实例,以及应该创建什么实例?

You used a space in 您在

da.Fill(ds, "Address Book") 

But no space in 但是里面没有空间

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

For the name of the table. 用于表的名称。 Therefore a table with name "AddressBook" does not exist. 因此,名称为“ AddressBook”的表不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM