VB.NET中的数据库访问 [英] Database Access in VB.NET
本文介绍了VB.NET中的数据库访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码访问了VB.NET中的数据库:
I have accessed a database in VB.NET using the following code:
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),这给了我一个例外,说应该创建对象的实例。我不明白到底是什么问题。我该如何创建实例,又该创建一个实例呢?
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. I am not understanding what exactly the problem is. How can I create an instance, and of WHAT should I create an instance?
推荐答案
您在
da.Fill(ds, "Address Book")
但是没有空格
txtFirstName.Text = ds.Tables("AddressBook").Rows(0).Item(1)
用于表的名称。因此,名称为 AddressBook的表不存在。
For the name of the table. Therefore a table with name "AddressBook" does not exist.
这篇关于VB.NET中的数据库访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文