【问题标题】:OLEDB Connection was unhandled errorOLEDB 连接未处理错误
【发布时间】:2016-01-14 14:48:54
【问题描述】:

我正在尝试使用 Visual Basic 和 OLEDB 将一些数据库记录链接到文本框和列表框,但是当我调试程序时,即使数据库位于 bin/debug 文件夹中,程序也无法识别它。

下面是代码:

Imports System.Data.OleDb
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source = Hospital.mdb"
        Dim dtPatientsIDs As New DataTable
        Dim daPatientIDs As New OleDbDataAdapter _
        ("Select [Patient ID] From Patients", ConnectString)

        daPatientIDs.Fill(dtPatientsIDs)
        lstPatientIDs.DataSource = dtPatientsIDs
        lstPatientIDs.DisplayMember = "Patient ID"

        OleDbDataAdapter1.Fill(DsPatientsAndWards1)
        txtWardName.DataBindings.Add("Text", DsPatientsAndWards1, _
                                 "Patients.Ward Name")
        txtWardType.DataBindings.Add("Text", DsPatientsAndWards1, _
                                 "Patients.Ward Type")



    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        BindingContext(DsPatientsAndWards1, "Patients").Position = _
        BindingContext(DsPatientsAndWards1, "Patients").Position - 1

    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        BindingContext(DsPatientsAndWards1, "Patients").Position = _
        BindingContext(DsPatientsAndWards1, "Patients").Position + 1
    End Sub

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Dim index As Short
        index = lstPatientIDs.SelectedIndex
        BindingContext(DsPatientsAndWards1, "Patients").Position = index

    End Sub
End Class

here is the error and its location

【问题讨论】:

  • 没有创建连接对象...这可能是异常消息的一部分。数据库可能最好保存到用户文件夹...部署时不会有 VS BIN 文件夹
  • 除非您告诉我们错误是什么以及错误在哪里,否则我们无法帮助您。
  • 此异常还有一个 InnerException 属性,其中包含更有意义的消息。请添加它
  • 错误现在位于问题的底部
  • 我将提供程序更改为 12.0,将源更改为 .accdb,而 hopsital 数据库仍在 bin/debug 文件夹中

标签: database vb.net windows oledb ole


【解决方案1】:

尝试改变

Dim daPatientIDs As New OleDbDataAdapter _
    ("Select [Patient ID] From Patients", ConnectString)

Dim Con as NEW OleDBConnection(ConnectionString)
Dim daPatientIDs As New OleDbDataAdapter _
    ("Select [Patient ID] From Patients", Con)

并添加

Con.open()

之前

    OleDbDataAdapter1.Fill(DsPatientsAndWards1)

【讨论】:

  • 我仍然收到完全相同的错误,即数据源不是有效路径
  • Hospital.mdb 是否存在于您的程序的 bin/debug/ 文件夹中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-27
  • 2019-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
相关资源
最近更新 更多