使用数据集显示访问中的数据 [英] Display data from access using dataset

查看:21
本文介绍了使用数据集显示访问中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As String
    dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data Source = C:\Users\Blessing\Documents\Ishvatest.mdb"
    con.Connectionstring = dbProvider & dbSource
    con.Open()
    sql = "SELECT * From Employees"
    da = New OleDb.OleDBDataAdapter(sql, con)
    da.Fill(ds, "Ishvatest")
    MsgBox("Ishvatest is now open")
    con.Close()
    MsgBox("Ishvatest is now closed")
    txtID.Text = ds.Tables("Employees").Rows(0).Item(1)
    txtID.Name = ds.Tables("Employees").Rows(0).Item(2)

如果我使用此代码运行程序,则会收到错误未处理空引用异常"

If I run the program with this code I get the error "Null Reference Exception was unhandled"

我的代码有什么问题?

推荐答案

好吧,假设表不为空,那么问题大概就在这里

Well, supposing that the table is not empty, then the problems is probably here

da.Fill(ds, "Ishvatest")

在这里您归档一个数据集并将其第一个表命名为Ishvatest",但随后您尝试从名为Employees"的表中读取数据.没有同名的表

Here you file a dataset and names its first table with the name "Ishvatest", but then you try to read from a table called "Employees". There is no table with that name

更改为(或 da.Fill(ds, "Employees"))

Change to (or da.Fill(ds, "Employees"))

txtID.Text = ds.Tables("Ishvatest").Rows(0).Item(1)
txtID.Name = ds.Tables("Ishvatest").Rows(0).Item(2)

另一种解决方案可能是使用索引来引用数据表而不是名称,并且(作为额外的预防措施)还检查您是否有任何记录要读取

Another solution could be to use an index to refer to the datatable instead of the name and (as an added precaution) checking also if you have any record to read

If ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
    txtID.Text = ds.Tables(0).Rows(0).Item(1)
    txtID.Name = ds.Tables(0).Rows(0).Item(2)
End If

这篇关于使用数据集显示访问中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆