搜索MS Access,并在文本框中显示VB.Net

时间:2018-09-12 14:25:15

标签: vb.net ms-access-2007

请协助。我希望从MS Access表中搜索ID号。 如果该ID号存在,我必须显示具有ID = TextBox.Text的人的名字,姓氏,性别和DOB,如下所示:

Patient_FirstName - txbFirstName
Patient_Surname - txbSurname
Patient_Sex - txbGender
Patient_DOB -txbDOB

此数据全部包含在名为Patient的表中。

我的代码如下:

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    dbProvider = "PROVIDER= Microsoft.ACE.OLEDB.12.0;"
    dbSource = "Data Source = C:\Users\Siyabonga Kubeka\Documents\Academic Folder\Richfield Graduate Institute Of Technology\BSC GUIDES\BSC GUIDES\Semester 2\Programming 512 Tasks\HealthServiceSystem.accdb"
    conn.ConnectionString = dbProvider & dbSource

    conn.Open()

    Dim dt As New DataTable
    Dim ds As New DataSet
    ds.Tables.Add(dt)
    Dim da As New OleDbDataAdapter

    da = New OleDbDataAdapter("Select * from Patient where ID like '%" & txbIdNumber.Text & "%'", conn)
    da.Fill(dt)

    txbFirstName.DataBindings.Add("Text", dt, "Patient.Patient_First_Name")
    txbSurname.DataBindings.Add("Text", dt, "Patient.Patient_Surname")
    txbDOB.DataBindings.Add("Text", dt, "Patient.Patient_DOB")
    txbGender.DataBindings.Add("Text", dt, "Patient.Patient_Sex")
End Sub

我收到此错误,但我无法弄清原因:

  

System.Data.OleDb.OleDbException:'未为一个或多个必需参数提供值。'

1 个答案:

答案 0 :(得分:0)

我确实知道是对的,请参见下面的代码:

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    dbProvider = "PROVIDER= Microsoft.ACE.OLEDB.12.0;"
    dbSource = "Data Source = C:\Users\Siyabonga Kubeka\Documents\Academic Folder\Richfield Graduate Institute Of Technology\BSC GUIDES\BSC GUIDES\Semester 2\Programming 512 Tasks\HealthServiceSystem.accdb"
    Dim strSql As String = "Select * from Patient where Patient_ID=@PatientID"
    conn.ConnectionString = dbProvider & dbSource
    conn.Open()
    Using cmd As New OleDbCommand(strSql, conn)
        cmd.Parameters.AddWithValue("@PatientID", txbIdNumber.Text)
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        While reader.Read()
            txbFirstName.Text += reader("Patient_First_Name").ToString()
            txbSurname.Text += reader("Patient_Surname").ToString()
            txbDOB.Text += reader("Patient_DOB").ToString()
            txbGender.Text += reader("Patient_Sex").ToString()
        End While
    End Using
End Sub