Domanda

I am using a csvReader in order to retrieve data from a csv file. My csv file consists of 500elements and I only need the first 300. How do I limit my csvReader in order to return only 300 elements?

 Dim PressureTable As DataTable = GetDataTabletFromCSVFile(BackUpDirDminus1)

    Console.WriteLine("Rows count:" + PressureTable.Rows.Count.ToString)
    Console.ReadLine()

    Using CSVReader As New TextFieldParser(BackUpDirDminus1)
        CSVReader.SetDelimiters(New String() {","})
        CSVReader.HasFieldsEnclosedInQuotes = True
        'read column names
        Dim colFields As String() = CSVReader.ReadFields()
        'For Each column As String In colFields

        While Not CSVReader.EndOfData
            Dim fieldData As String() = CSVReader.ReadFields()
            'Making empty value as null
            For i As Integer = 0 To fieldData.Length-1
                If fieldData(i) = "" Then
                    fieldData(i) = Nothing
                End If
            Next
         PressureTable.Rows.Add(fieldData)
        End While
    End Using

Please help. Thanks

È stato utile?

Soluzione

I suppose there should be a method name "ReadNextRecord()", so your while loop should be like

While CSVReader.ReadNextRecord()

Declare a int k =0 and do k++

Once K++ reaches 300, then you can End While.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top