【问题标题】:Unable to read even though the Employee ID is correct即使员工 ID 正确也无法读取
【发布时间】:2022-01-20 00:57:04
【问题描述】:

我正在尝试创建一个表单,您可以在其中输入员工代码。即使 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()

【问题讨论】:

标签: vb.net ms-access


【解决方案1】:

请按此顺序检查以下内容:-

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

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

  • c) 如果你到达第三步,那么检查桌子是否有更多 对于相同的员工代码,多于 1 行 - RecordCount > 0。如果 在这种情况下,记录集返回 > 1 行,然后请处理错误 场景

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

  • e) 如果仍有问题,请尝试移除饰边并尝试 输入正确的员工代码并检查是否可以正常工作 这个特殊的场景。

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

【讨论】:

  • 用户在 VB.net 中编码,所以不要认为 db 数据库变量是相关的。
  • db.OpenRecordset 是用于打开 DAO 类型记录集的 VBA,我认为 VB.net 使用 ADO。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    您应该始终使用 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以匹配数据库列的实际类型。

    【讨论】:

    • 成功了,非常感谢!
    猜你喜欢
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多