有人可以告诉我如何在我的CSV文件中搜索两个值吗?我的CSV文件如下所示:
1,"Garry","Tall","4545"
2,"Julius", "Short", "2564"
我想确认4545号码与同一行中的Garry名称相匹配。例如,用户可以输入4545并且名称Garry和代码将检查csv是否匹配这两个值。但名称和数字必须匹配,而不仅仅是单个值。
我不确定如何将csv文件加载到我的visual basic或如何搜索它。所以任何帮助将不胜感激。过去两个小时我一直在网上四处寻找,但到目前为止我似乎没有任何工作。
Public Function CheckRegistrationKey(ByVal Name As String, ByVal Number As Integer)
Dim filepath As String = My.Computer.FileSystem.SpecialDirectories.Desktop("\File1.csv")
Dim Key As String
Dim NameToSearhFor As String = Name
Dim NumberToSearchFor As Integer = Number
'Load file
'Search file for values
'return true if found
'return false if not found
End Function
更新
'Load file
Using MyReader As New FileIO.TextFieldParser(filepath)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
'MsgBox(currentField)
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
End Try
End While
End Using
现在我只需要弄清楚如何搜索这些值。
答案 0 :(得分:1)
如果您只需要检查一个用户一次,那么您建议的代码就足够了,但是如果您需要多次检查用户,那么更像这样的东西会更好:
Option Infer On
Option Strict On
Module Module1
Dim data As List(Of datum)
Public Class datum
Property ID As Integer
Property FirstName As String
Property LastName As String
Property Key As Integer
End Class
Function LoadData(srcFile As String) As List(Of datum)
Dim data As New List(Of datum)
Using tfp As New FileIO.TextFieldParser(srcFile)
tfp.TextFieldType = FileIO.FieldType.Delimited
tfp.SetDelimiters(",")
tfp.HasFieldsEnclosedInQuotes = True
Dim currentRow As String()
Dim currentLine = 0
While Not tfp.EndOfData
currentLine += 1
Try
currentRow = tfp.ReadFields()
If currentRow.Length = 4 Then
data.Add(New datum With {.ID = CInt(currentRow(0)), .FirstName = currentRow(1), .LastName = currentRow(2), .Key = CInt(currentRow(3))})
End If
Catch ex As Exception
MsgBox($"Error in file {srcFile} at line {currentLine}: {ex.Message}")
End Try
End While
End Using
Return data
End Function
Function IsValidUser(firstname As String, key As Integer) As Boolean
If data Is Nothing Then
Throw New Exception("Data has not been initialised.")
End If
Return data.Any(Function(d) d.FirstName = firstname AndAlso d.Key = key)
End Function
Sub Main()
Dim srcFile = "C:\temp\sampledata2.txt"
data = LoadData(srcFile)
For Each user In {"Garry", "Harry"}
Console.WriteLine($"Found {user}: " & IsValidUser(user, 4545).ToString())
Next
Console.ReadLine()
End Sub
End Module
答案 1 :(得分:0)
它可能不是最有效的方式,但我已经让我的代码工作如下所述。
'Load file
Using MyReader As New FileIO.TextFieldParser(filepath)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim LastName As String = currentRow(1)
Dim Key As String = currentRow(4)
If LastName = "Garry" And Key = "6565" Then
'id correct
Else
'id wrong
End If
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
'Error code here
End Try
End While
End Using