从VB.NET从MSAccess数据库中检索数据

时间:2017-09-22 09:14:42

标签: vb.net ms-access

我想从数据库中检索数据。我使用MSAccess作为数据库。我在这里面临的问题是我无法通过vb.net从MSAccess获取数据。

查找我正在使用的下面的代码段。

Imports System.Data.OleDb
Public Class Form1
    Dim cn As OleDb.OleDbConnection
    Dim ds As DataSet
    Dim da As OleDbDataAdapter
    Dim tables As DataTableCollection
    Dim source1 As New BindingSource
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim connectionString As String

        connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                "Data Source=C:\myfolder\fdfd.accdb;"

        cn = New OleDbConnection

        cn.ConnectionString = connectionString


        ds = New DataSet
        tables = ds.Tables
        da = New OleDbDataAdapter("Select * From names", cn)
        da.Fill(ds, "names")


        Dim view As New DataView(tables(0))
        source1.DataSource = view
        DataGridView1.DataSource = view
    End Sub
End Class

错误发生在以下行:da.Fill(ds, "names")

这是错误截图:

Error Screenshot

先谢谢

2 个答案:

答案 0 :(得分:1)

基于equisde's answer,我提出了以下适用于我的代码:

Imports System.Data.OleDb
Public Class Form1
    Dim cn As OleDb.OleDbConnection
    Dim ds As DataSet
    Dim da As OleDbDataAdapter
    Dim tables As DataTableCollection
    Dim source1 As New BindingSource
    Dim oleCommand As OleDbCommand
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
       cn = New OleDbConnection

        If System.IO.File.Exists("C:\myfolder\fdfd.accdb") Then
            cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myfolder\fdfd.accdb; Persist Security Info=False;"
        End If
        Try
            Dim SQL As String = "SELECT * from [names]"

            oleCommand = New OleDbCommand(SQL, cn)
            da = New OleDbDataAdapter(oleCommand)
            Dim table = New DataTable("[names]")
            cn.Open()
            da.Fill(table)
            cn.Close()
            Dim view As New DataView(table)
            source1.DataSource = view
            DataGridView1.DataSource = view

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class   

答案 1 :(得分:0)

尝试在填充数据集之前打开连接

Imports System.Data.OleDb

Public Class Form1

Dim cn As OleDb.OleDbConnection
Dim ds As DataSet
Dim da As OleDbDataAdapter
Dim tables As DataTableCollection
Dim source1 As New BindingSource

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    cn = New OleDbConnection

    cn.ConnectionString =  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myfolder\fdfd.accdb; Persist Security Info=False;"

    Try

        ds = New DataSet
        tables = ds.Tables
        cn.Open()
        da = New OleDbDataAdapter("SELECT * FROM [Names]", cn)
        da.Fill(ds, "names")
        cn.Close()
        Dim view As New DataView(tables(0))
        source1.DataSource = view
        DataGridView1.DataSource = view

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
End Class