获取数据MS Access然后将其放在VB NET文本框中

时间:2014-04-06 15:52:04

标签: sql vb.net ms-access-2007

我试图弄清楚我的代码有什么问题, 我试图提取从下拉列表中选择的员工ID的值,然后获取该员工的所有信息并将其放在文本框vb.net表单中,但每当我选择员工ID时都会出现错误  它给了我一条错误信息:"类型为#System; Data.OleDb.OleDbException'的未处理异常。发生在System.Data.dll

附加信息:没有给出一个或多个必需参数的值。"

因此突出读者,我不确定问题是什么,我已经初步化了读者......请指导我。感谢

Private Sub eNumText_SelectedIndexChanged(sender As Object, e As EventArgs) Handles eNumText.SelectedIndexChanged


        Dim dbSource = "Data Source= C:\Databse\Company_db.accdb"
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= c:\Databse\Company_db.accdb"

        Dim sqlQuery As String
        Dim sqlCommand As New OleDbCommand
        Dim sqlAdapter As New OleDbDataAdapter
        Dim Table As New DataTable

        Dim empNum As String
        Dim empFname As String
        Dim empLname As String
        Dim empDept As String
        Dim empStat As String
        Dim empYears As String

        empNum = eNumText.Text
        empFname = empFnameText.Text
        empLname = empLnameText.Text
        empDept = DeptText.Text
        empStat = StatText.Text
        empYears = yearstext.Text

        sqlQuery = "SELECT * FROM tbl_empinfo WHERE EmpID like empNum"

        With sqlCommand
            .CommandText = sqlQuery
            .Connection = con
            .Parameters.AddWithValue("EmpID", empNum)
            With sqlAdapter
                .SelectCommand = sqlCommand
                .Fill(Table)
            End With
            With DataGridView1
                .DataSource = Table
            End With
        End With

        Dim path = "Data Source= C:\Databse\Company_db.accdb"
        Dim command = "SELECT * FROM tbl_empinfo WHERE EmpID like empNum"
        QueryData(path, Command)


        con.Close()

    End Sub
    Private Sub QueryData(PathDb As String, command As String)
        PathDb = "Data Source= C:\Databse\Company_db.accdb"
        Using connection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathDb)
            Using da As New System.Data.OleDb.OleDbCommand(command, con)
                con.Open()

                Dim reader = da.ExecuteReader()
                If reader.Read() Then
                    empFnameText.Text = reader("FirstName")
                    empLnameText.Text = reader("LastName")
                End If

                con.Close()
            End Using
        End Using
    End Sub

1 个答案:

答案 0 :(得分:1)

您需要在参数empNum前加上@:@empNum。请参阅此答案:How to pass a parameter from vb.net以获取示例。