检索自动编号主键字段

时间:2017-05-24 02:40:05

标签: vb.net ms-access

我有这个功能来检索Employees表中的员工详细信息。我正在使用vb.net 2012和MS Access作为我的数据库。我的问题是如何使用自动编号和主键属性/数据类型的ID来检索员工详细信息?这是我的代码:

Public Sub DisplayEmployeeDetail()

    Try
        sqlEmployeeInfo = "SELECT * FROM tblEmployees WHERE tblEmployees.ID = " + txtDTRidnum.Text + ";"
        Dim da As OleDb.OleDbDataAdapter = New OleDbDataAdapter(sqlEmployeeInfo, con)
        Dim ds As New DataSet
        da.Fill(ds, "tblEmployees")
        Dim temp = ds.Tables("tblEmployees").Rows.Count
        For i = 0 To temp - 1
            lblFname.Text = CStr(ds.Tables("tblEmployees").Rows(i).Item("FirstName"))
            lblLname.Text = CStr(ds.Tables("tblEmployees").Rows(i).Item("LastName"))
            lblMname.Text = CStr(ds.Tables("tblEmployees").Rows(i).Item("MiddleName"))
            lblAddress.Text = CStr(ds.Tables("tblEmployees").Rows(i).Item("Address"))
            lblPosition.Text = CStr(ds.Tables("tblEmployees").Rows(i).Item("Position"))
        Next
    Catch ex As Exception
        MessageBox.Show("Error in load: " & ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

End Sub

它给出了System.Data.OleDb.Exception:查询表达式中的语法错误(缺少运算符)&t; tblEmployees.ID ='

2 个答案:

答案 0 :(得分:0)

使用这样的单引号:

sqlEmployeeInfo = "SELECT * FROM tblEmployees WHERE tblEmployees.ID = '" + txtDTRidnum.Text + "';"

另外,即使您不想使用,也可以试试这个:

Private Sub getData()
        Dim empDA As OleDbDataAdapter = New OleDbDataAdapter()
        Dim empDS As DataSet = New DataSet
        Dim empTable As DataTable = New DataTable("EMPLOYEES")
        Dim adoConn As ADODB.Connection = New ADODB.Connection()
        Dim adoRS As ADODB.Recordset = New ADODB.Recordset()

        Try
            empTable.Columns.Add("FirstName", Type.GetType("System.String"))
            empTable.Columns.Add("LastName", Type.GetType("System.String"))
            empTable.Columns.Add("MiddleName", Type.GetType("System.String"))
            empTable.Columns.Add("Address", Type.GetType("System.String"))
            empTable.Columns.Add("Position", Type.GetType("System.String"))
            empDS.Tables.Add(empTable)

            'Use ADO objects from ADO library (msado15.dll) imported
            ' as .NET library ADODB.dll using TlbImp.exe

            'Specify your connection string here
            adoConn.Open("Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;", "", "", -1)
            adoRS.Open("SELECT * FROM tblEmployees WHERE tblEmployees.ID = " + txtDTRidnum.Text + ";", adoConn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1)
            empDA.Fill(empTable, adoRS)

            Dim temp = empTable.Rows.Count
            For i = 0 To temp - 1
                lblFname.Text = CStr(empTable.Rows(i).Item("FirstName"))
                lblLname.Text = CStr(empTable.Rows(i).Item("LastName"))
                lblMname.Text = CStr(empTable.Rows(i).Item("MiddleName"))
                lblAddress.Text = CStr(empTable.Rows(i).Item("Address"))
                lblPosition.Text = CStr(empTable.Rows(i).Item("Position"))
            Next

            adoRS.Close()
            adoConn.Close()
        Catch ex As Exception
            MessageBox.Show("Error in load: " & ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            adoRS.Close()
            adoConn.Close()
        End Try
    End Sub

此示例Here

答案 1 :(得分:0)

发生此错误是因为文本字段txtDTRidnum.Text为空。

相关问题