Nilesh
Nilesh

Reputation: 135

How to create DataTable from csv file content

I have string data containing *.csv file content (read from using File.ReadAllText(filePath) method and want create new Data Table object from the string data. I don't have an option to use file path because the file is immediately deleted from the drive once it is been read.

Upvotes: 0

Views: 526

Answers (1)

shree.pat18
shree.pat18

Reputation: 21757

You could consider replacing your File.ReadAllText method with File.ReadAllLines. Then, you could do something like the below steps:

  1. Create a datatable, define its structure etc.
  2. Read all lines from file as an array of strings
  3. In a loop, split the string by your separator character, then parse each portion of string as its corresponding datatype e.g. int.TryParse() for numerical values etc.
  4. Add new row to the datatable using DataTable.Rows.Add and supplying an object array with your parsed values.

Upvotes: 0

Related Questions