简体   繁体   English

如何遍历数据表中的行?

[英]How to loop through rows in a Datatable?

I'm having some trouble getting a loop to see more then just the first row of data. 我在获取循环以查看更多内容而不是仅第一行数据时遇到了一些麻烦。 The referenced dataset function gets all the required rows with no problem therefore I'm sure the problem must be with the code. 引用的数据集函数可以毫无问题地获取所有必需的行,因此,我确定问题一定出在代码上。

Dim dtLogin As System.Data.DataTable
    Dim userDetails As New dsMembersTableAdapters.mi_membersTableAdapter
    Dim rowsLogin As System.Data.DataRow

    'Fill datatable using method from dataset
    dtLogin = userDetails.GetUserData()

    'Find cotrols hidden in Login View
    Dim user As String = txtUser.Text
    Dim pass As String = txtPass.Text

    'Search all users
    For Each rowsLogin In dtLogin.Rows
        'Find Username Entered
        If user = dtLogin.Rows.Item(0).Item(1) Then
            'Checks users password matches
            If pass = dtLogin.Rows.Item(0).Item(2) Then
                If dtLogin.Rows.Item(0).Item(6) = 1 Then
                    'Log User In
                    FormsAuthentication.RedirectFromLoginPage(dtLogin.Rows.Item(0).Item(1), True)
                Else
                    'Account Not Active Message
                    lblValidation.Text = "There is a problem with your account, please contact the website administration"
                End If
            Else
                'Incorrect Password Message
                lblValidation.Text = "Incorrect Password"
            End If
        Else
            'No User in DB Message
            lblValidation.Text = "No User Found" + dtLogin.Rows.Item(0).Item(1)
        End If
    Next

If anyone could help at all or point me in the rihgt direct that would be fantastic! 如果有人可以提供任何帮助或在rihgt中指出我的意思,那太好了! Thanks in advance :) 提前致谢 :)

when you use For Each rowsLogin In dtLogin.Rows you are telling the compiler that, for each dtLogin.Rows item, assign it into the variable rowsLogin . 当您For Each rowsLogin In dtLogin.Rows使用For Each rowsLogin In dtLogin.Rows您要告诉编译器,对于每个dtLogin.Rows项目,将其分配给变量rowsLogin

So, every time, inside the loop, you stop using dtLogin.Rows.Item(0).Item(2) like in If pass = dtLogin.Rows.Item(0).Item(2) Then but rather If pass = rowsLogin.Item(0).Item(2) Then 因此,每次在循环内,您都将停止使用dtLogin.Rows.Item(0).Item(2)If pass = dtLogin.Rows.Item(0).Item(2) Then ,而If pass = rowsLogin.Item(0).Item(2) Then

dtLogin.Rows.Item(0).Item(1) - the (0) after Rows.Item refers to the index in the collection of rows, so you're always looking at the first row. dtLogin.Rows.Item(0).Item(1) -Rows.Item之后的(0)引用行集合中的索引,因此您总是在看第一行。

Instead of using dtLogin.Rows.Item(0).Item(1) , etc. in your loop, use rowsLogin.Item(1) . 不要在循环中使用dtLogin.Rows.Item(0).Item(1)等,而应使用rowsLogin.Item(1)

dim bUserFound as boolean = false       
For Each rowsLogin In dtLogin.Rows
            'Find Username Entered
            If user = rowsLogin(1) Then
bUserFound = true
                'Checks users password matches
                If pass = rowsLogin(2) Then
                    If rowsLogin(6) = 1 Then
                        'Log User In
                        FormsAuthentication.RedirectFromLoginPage(rowsLogin(1), True)
                    Else
                        'Account Not Active Message
                        lblValidation.Text = "There is a problem with your account, please contact the website administration"
                    End If
                Else
                    'Incorrect Password Message
                    lblValidation.Text = "Incorrect Password"
                End If
            Else
                'No User in DB Message
               ' lblValidation.Text = "No User Found" + rowsLogin(1)

            End If
        Next 

if not bUserFound then
lblValidation.Text = "No User Found"
end if

For more clear code you should use rowsLogin("USER_NAME") instead of rowsLogin(1), rowsLogin("USER_PWD") instead of rowsLogin(2), etc. 为了获得更清晰的代码,您应该使用rowsLogin(“ USER_NAME”)代替rowsLogin(1),rowsLogin(“ USER_PWD”)代替rowsLogin(2)等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM