我的代码只读取了我的csv文件中的第一行记录

时间:2014-01-22 21:30:18

标签: arrays vb.net csv linear

所以,这只是读取我的csv文件中的第一行记录。我最终希望它能够阅读所有内容并将其与用户输入进行比较,如果正确的话。 我非常感谢任何形式的帮助和建议。

赞赏,

  For Each line As String In File.ReadAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls\stdnt&staffdtls.csv")
        Dim item() As String = line.Split(","c)
        If (enteredusername = item(0)) And (enteredpassword = item(1)) Then
            Console.WriteLine()
            Console.Clear()
            Console.WriteLine("Welcome," & item(3) & item(4))
            Threading.Thread.Sleep(1000)
            Console.Clear()
        Else
            Console.WriteLine()
            Console.ForegroundColor = ConsoleColor.Red
            Console.WriteLine("Sorry, username or password not found, please try 
        again.")
            Console.ResetColor()
            Main()
        End If

        If item(2) = "p" Then
            pupilmenu()
        ElseIf item(2) = "s" Then
            staffmenu()
        ElseIf item(2) = "a" Then
            adminmenu()
        End If
    Next

1 个答案:

答案 0 :(得分:1)

看起来您的问题是您在For循环中拥有所有逻辑。很难肯定地说,你需要做什么,而不知道更多细节,但我怀疑它可能更像是这样:

Dim foundItem() as String = Nothing
For Each line As String In File.ReadAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls\stdnt&staffdtls.csv")
    Dim item() As String = line.Split(","c)
    If (enteredusername = item(0)) And (enteredpassword = item(1)) Then
        foundItem = item
        Exit For
    End If
Next
If foundItem IsNot Nothing Then
    Console.WriteLine()
    Console.Clear()
    Console.WriteLine("Welcome," & item(3) & item(4))
    Threading.Thread.Sleep(1000)
    Console.Clear()
    If foundItem(2) = "p" Then
        pupilmenu()
    ElseIf foundItem(2) = "s" Then
        staffmenu()
    ElseIf foundItem(2) = "a" Then
        adminmenu()
    End If
Else
    Console.WriteLine()
    Console.ForegroundColor = ConsoleColor.Red
    Console.WriteLine("Sorry, username or password not found, please try again.")
    Console.ResetColor()
    Main()
End If