【问题标题】:Retrieving data from MSAccess Database from VB.NET从 VB.NET 从 MS Access 数据库中检索数据
【发布时间】:2023-04-04 22:29:01
【问题描述】:

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

在下面找到我正在使用的代码 sn-p。

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")

这是错误截图:

提前致谢

【问题讨论】:

    标签: vb.net ms-access


    【解决方案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   
    

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

      • 我已经尝试了您建议的 sn-p,但发生了同样的错误。有没有其他方法可以从 MSAccess 访问数据??
      • 代码正确。尝试使用try/catch 块来捕获异常。请参阅编辑的代码。也许您的数据库丢失或您的连接字符串错误。
      • 连接字符串是正确的,但是不知道为什么会出现这个错误并且数据库也存在
      • 使用try/catch 块将清除您的疑虑。它会在对话框中显示详细的错误消息。
      • IErrorInfo.GetDescription 失败,出现 E_FAIL(0x80004005)。 以下是我收到的消息框。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-17
      • 2021-12-13
      • 2023-04-08
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多