即使员工 ID 正确也无法读取

Unable to read even though the Employee ID is correct

我正在尝试创建一个表单,您可以在其中输入员工代码。即使 Employee ID 是正确的,它仍然被错误读取。我该如何解决?这是代码。

con.Open()

If sql = "Select * from tblempinfo WHERE EmpID ='" & Trim(TextBox1.Text) & "'" Then

    MsgBox("You are now logged in ")
    Me.Hide()
    tito.Show()
Else
    MsgBox("Invalid Employee Code")
End If

con.Close()

请依次检查以下内容:-

  1. 如果代码没有出错那么-
  • a) 首先打开您尝试访问的 SQL 的记录集 使用 OpenRecordset()

  • b) 如果记录集上没有数据,验证错误场景 IsNull()

  • c) 如果你到达第三步然后检查 table 是否有更多 同一员工代码的行数多于 1 行 - RecordCount > 0。如果 record set returns > 1 rows in this case,请处理错误 情景

  • d) 以相同的顺序处理代码中的上述每个步骤, 请不要试图直接到达最终状态,因为它会 帮助您一起理解代码和数据流。

  • e) 如果仍然存在问题,请尝试删除 trim 并尝试 输入正确的员工代码并检查是否正常工作 这种特殊情况。

  1. 如果是案例代码出错,请修复代码并重试。
  2. 您提到数据读取错误,这似乎表明没有错误,但返回的数据不正确。因此,请提供有关您的输入和输出的更多信息,以便提供更多建议。但我还是建议你按照上面建议的方法处理。

您必须执行查询并获得结果。使用结果计数更有效,这意味着只需要传递一个数字。

您应该始终使用 SQL 参数将值传递给查询,而不是将它们直接放在查询字符串中。在下面的代码中,参数在查询中由 ? 表示 - 代码不使用参数的名称(“@empid”),但它可以帮助您了解哪个是哪个不止一个

Dim sql = "SELECT COUNT(*) FROM tblempinfo WHERE EmpID = ?"
Dim n = 0

Using conn = New OleDbConnection("yourConnectionString"),
       cmd = New OleDbCommand(sql, conn)

    Dim empId = TextBox1.Text.Trim()
    cmd.Parameters.Add("@empid", OleDbType.VarWChar).Value = empId

    conn.Open()
    n = CInt(cmd.ExecuteScalar())

End Using

If n = 1 Then
    MsgBox("You are now logged in ")
    Me.Hide()
    tito.Show()
Else
    MsgBox("Invalid Employee Code")

End If

请根据需要调整OleDbType.VarWChar以匹配数据库列的实际类型。

您可能需要检查用户的身份验证级别,并确保只有一条记录。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim input = TextBox1.Text.Trim()
    Dim empID As Integer
    ' need to validate the text input as integer
    If Integer.TryParse(input, empID) AndAlso isUserAuthenticated(empID) Then
        MsgBox("You are now logged in ")
        Me.Hide()
        tito.Show()
    Else
        MsgBox("Invalid Employee Code") ' TextBox.Text incorrect format
    End If
End Sub

Private Function isUserAuthenticated(empID As Integer) As Boolean
    Using con As New SqlClient.SqlConnection("connection string") ' put your connection string here
        Dim sql = "Select * from tblempinfo WHERE EmpID = @empID"
        Using com As New SqlClient.SqlCommand(sql, con)
            con.Open()
            com.Parameters.AddWithValue("@empID", empID)
            Dim dr = com.ExecuteReader()
            Dim results As New List(Of Object)()
            While dr.Read()
                results = dr.GetValue(0) ' index of result you will use to authenticate user
            End While
            If results.Any() Then
                If results.Count = 1 Then
                    Dim result = DirectCast(results(0), Integer) ' assuming the result is an integer
                    If result > 5 Then ' arbitrary here, but assuming > 5 means authenticated
                        Return True
                    Else
                        Return False ' not authenticated
                    End If
                Else
                    Return False ' > 1 record
                End If
            Else
                Return False ' no records
            End If
        End Using
    End Using
End Function