2016-11-06 59 views
0

有人可以告訴我如何搜索我的CSV文件的兩個值?我的CSV文件看起來像這樣:如何搜索CSV文件的兩個值(VB)

1,"Garry","Tall","4545" 
2,"Julius", "Short", "2564" 

我想確認的是,4545號相匹配的名稱加里同一排。例如,用戶可以輸入4545和名字Garry,代碼會檢查csv是否匹配這兩個值。但名稱和編號必須匹配,而不僅僅是一個值。

我不知道如何將csv文件加載到我的視覺基本或如何搜索它。所以任何幫助將不勝感激。過去兩個小時,我一直在網上瀏覽,但到目前爲止,似乎沒有任何工作能夠幫助我。

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

您可以使用[TextFieldParser Class](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(v = vs.110).aspx)加載數據。我建議爲每個數據項(ID,Firstname,Lastname,Key)創建一個類,併爲所有行使用該類的List。 –

回答

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 
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