1

我的目标是根据用户输入定位主键。

表格内容如下:UserID,account,password。

userID 设置为主键和整数类型。

account 设置为 varchar 类型。

密码设置为 varchar 类型。

这是我目前验证用户输入的代码。

Dim account As String = txtAcc.Text
    Dim password As String = txtPss.Text


    For Each Me.dr In atadap.GetData
        If dr.Item(1) = account And dr.Item(2) = password Then
            **[retrieve userID for specified data row]**
            MsgBox("Access granted!")
            Me.Hide()
            frmMenu.Show()
            Exit For
        Else
            MsgBox("Access denied!")
            txtAcc.Text = ""
            txtPss.Text = ""
            Exit For
        End If
    Next

以下是我在研究中使用的链接:http: //msdn.microsoft.com/en-us/library/y06xa2h1.aspx

但仍然无法弄清楚如何做到这一点。任何帮助将不胜感激。提前谢谢了。

4

2 回答 2

0

做:

userId = dr.Item(0)

回身份证?

于 2013-05-03T21:59:35.180 回答
0

您在第一个数据行上退出For each循环,这就是为什么除非记录位于数据表的顶部,否则您将永远找不到结果。

我不知道为什么你需要阅读每一行来检查特定的行。但是,如果这是您的要求,请将您的代码更改为:

Dim account As String = txtAcc.Text
Dim password As String = txtPss.Text
Dim userFound as Boolean = False
Dim userId as Integer

For Each Me.dr In atadap.GetData
    If dr.Item(1) = account And dr.Item(2) = password Then
        userId = dr.Item(0) ' save the userId in a variable
        userFound = True
        Exit For
    End If
Next

If userFound then
    MsgBox("Access granted! Your User ID is " & userId)
    Me.Hide()
    frmMenu.Show()
Else
    MsgBox("Access denied!")
    txtAcc.Text = ""
    txtPss.Text = ""
End If
于 2013-05-04T01:15:39.903 回答