Importing from CSV file, how to skip first X rows

I am writing an application to process and upload a csv file to a remote server. The application needs to be fairly flexible, allowing custom mappings of columns.

The first big hurdle I've come to is with reading the file, many of the files have the data starting at row 5+ with some junk data at the top, example below:

Account: 123
Date: 15/12/2011
Type: Full

Ideally I'd like a way to pick up the first line of data but this might be tricky.

I'm connecting to the file using Jet with the connection string:

If (_extension = ".csv") Then

                _excelconn.ConnectionString = String.Format( _
                    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" & _
                    "; Extended Properties=""text;HDR=No;FMT=Delimited""", _directory)

End If

Then getting data via the code:

If ValidateAll() Then

                ConnectExcel() ' Opens connection to csv/excel file

                _excelcommand.CommandText = "SELECT * FROM [" & _excelsheets(_selsheet) & "]" ' Currently selected sheet, returns SELECT * FROM [1234#csv]
                _excelcommand.Connection = _excelconn

                _reader = _excelcommand.ExecuteReader()

                While _reader.Read()


                End While

            Catch ex As Exception
                retval = -1
            End Try

        End If

This is falling over with an exception as I can't access reader(2) for the first lines.

Is there any way in the connection string or in the SELECT statement to tell Jet to skip the first X rows of the sheet? If not is there a way to only process the reader.Read() if the row number is higher than X?

I know it's probably possible with reading the entire file to an array and parsing from there or by copying the file contents to a new csv file but if there is a way to avoid that and do it through jet/ado then I'd much prefer that!

(tagged sql as there might be an sql solution in the ado query)


Found the answer after a lot of experiments and searches, moved to a table adaptor, code below:

Private _filepath As String
Private _directory As String
Private _filename As String
Private _extension As String
Private _selsheet As Integer
Private _skiprows As Integer
Private _data As New DataTable

Private _excelconn As New OleDbConnection
//Private _reader As OleDbDataReader
Private _excelcommand As New OleDbCommand
Private _excelta As New OleDbDataAdapter

_excelcommand.CommandText = "SELECT * FROM [" & _excelsheets(_selsheet) & "]"
_excelcommand.Connection = _excelconn

_excelta.SelectCommand = _excelcommand
_data = New DataTable
_excelta.Fill(_skiprows, Int32.MaxValue, _data)

For i = 0 To _data.Columns.Count - 1
    If _data.Rows(0).Item(i).ToString <> "" And IsNothing(_data.Columns(_data.Rows(0).Item(i).ToString)) Then
          _data.Columns.Item(i).ColumnName = _data.Rows(0).Item(i).ToString
    End If

_data.Rows.RemoveAt(0) //removes first data row containing labels

Need Your Help

Hide data atribute with css3

jquery css3

Working on my website, I'm adding some functions like the one provided with Snap.js (adds a lateral menu, like facebook one).

DIV inline-block + width 100%

css css-tables

I can't display a few DIV's in one line. display: inline-block and float: left doesn't work. My site width is not fixed so I want it to be dynamic to fit any width of screen.