dcsimg
Results 1 to 5 of 5

Thread: Use first row (Heading) of csv filel as my varibale names.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2019
    Posts
    20

    Use first row (Heading) of csv filel as my varibale names.

    text file

    car,driver,payee
    1X,Bill Gates,Microsoft
    T7,Tiger Woods,Nike Corp
    ...

    Code:
           Dim sr As StreamReader = New StreamReader("c:\export\list.csv")
            Do While sr.Peek <> -1
                Dim line As String = sr.ReadLine
                Dim words() As String = line.Split(New Char() {","c})
          
                Dim x As New Racer
                x.Car = words(0)
                x.Driver = words(1)
                x.Payee = words(2)
    ...
    I have many text file that I will open with a file dialog box.
    instead of having "Hard Codeing" in the variables (x.car, x.Driver, x.Payee) I would like these to be the fields name in the header row of the text file.


    ie:
    Club,Player,sponsor
    1X,Bill Gates,Microsoft
    T7,Tiger Woods,Nike Corp
    ...

    in this example I would want >>> x.Club, x.Player, x.sponsor

    any ideas,

    Thanks,
    Ross

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,004

    Re: Use first row (Heading) of csv filel as my varibale names.

    Is there a reason you're not using ADO.Net/Jet driver to read the csv as a standard delimited datasource?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2019
    Posts
    20

    Re: Use first row (Heading) of csv filel as my varibale names.

    Quote Originally Posted by kpmc View Post
    Is there a reason you're not using ADO.Net/Jet driver to read the csv as a standard delimited datasource?
    2 reasons)
    1. I've read that there is a problem with it importing data when a column contains both numeric and alpha data (something about setting IMEX=1 and it still wont fix it this issue.)
    2. I don't know how to set it up.

  4. #4
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,004

    Re: Use first row (Heading) of csv filel as my varibale names.

    Maybe a TextFieldParser would be more your style. You would read the first row as column names (header row)then the whileloop would read the remaining as rows.

    As for the IMEX thing, from my experience you can omit that setting all together as its does nothing. Just read all the data in as string and do conversion as necessary.

  5. #5
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,004

    Re: Use first row (Heading) of csv filel as my varibale names.

    On another note, if you take the OleDb route you can define all the column datatype in a Schema.ini file, which works only for text based delimited sources.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width