我正在尝试将一些数据库记录链接到文本框和带有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
答案 0 :(得分:0)
尝试更改
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)