如何遍历数据表中的行?
我有一些麻烦得到一个循环,看看更多,然后只是第一行数据。所引用的数据集函数可以毫无问题地获得所有必需的行,因此我确定问题必须出现在代码中。如何遍历数据表中的行?
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
如果任何人都可以帮助我,或者直接指向我的rihgt直接,那太棒了!在此先感谢:)
dtLogin.Rows.Item(0).Item(1)
- 在(0)Rows.Item指索引行的集合中后,让你”总是在看第一排。
而不是在您的循环中使用dtLogin.Rows.Item(0).Item(1)
等,使用rowsLogin.Item(1)
。
这就像一个魅力工作 - 谢谢:) – 2013-02-12 02:09:44
当您使用For Each rowsLogin In dtLogin.Rows
您告诉编译器,对于每个dtLogin.Rows
项目,将其分配到变量rowsLogin
。
所以,每一次,在循环中,你停止使用dtLogin.Rows.Item(0).Item(2)
像If pass = dtLogin.Rows.Item(0).Item(2) Then
而是If pass = rowsLogin.Item(0).Item(2) Then
感谢您的帮助,但这只是产生一个错误 - “未找到类型为”Integer“的公共成员'Item'。” – 2013-02-12 02:07:27
我会假设你正在尝试这样做是通过名称获得项目,所以我会使用'rowsLogin [“my_column_name”]'而不是你拉出值的方式 – balexandre 2013-02-12 02:10:04
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
为了更清晰的代码,你应该使用 rowsLogin( “USER_NAME”),而不是rowsLogin(1), rowsLogin( “USER_PWD”),而不是rowsLogin(2)等
张贴的答案看起来是正确的......但为什么你循环所有的用户数据来验证一个用户? – 2013-02-12 01:58:05
尝试从不使用索引...如果您更改了选择语句的顺序,您的所有代码都将被破坏...请参阅我对我的回答的评论,并使用行[]'代替。 –
balexandre
2013-02-12 02:16:08