Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the code below to read data from a csv file. But the string values can't be read where the columns which are considered as a number column.
For example, there is a column which contains a string value "AAA", and the rest of this column contains number value such as 1,2,3. So when I read it, the string value will be read as "" instead of correct result "AAA".

please tell me how to deal with this problem, thanks.

//Fetch the location of CSV file 

string filePath = Server.MapPath("UploadedCSVFiles") + "\\";

string strSql = "SELECT * FROM [" + fileInfo.Name + "]";

string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";" + "Extended Properties='text;HDR=YES;'";



// load the data from CSV to DataTable 



OleDbDataAdapter adapter = new OleDbDataAdapter(strSql, strCSVConnString);

DataTable dtCSV = new DataTable();

DataTable dtSchema = new DataTable();


adapter.FillSchema(dtCSV, SchemaType.Mapped);

adapter.Fill(dtCSV);


And I wonder if I can set the Jet to let all the columns are considered as string data type. And after select , I convert the string to number.
Posted
Updated 14-Oct-11 13:15pm
v2

I would try using this FileHelpers Library[^] rather than dealing with the Jet
 
Share this answer
 
I prefer to manipulate CSV files manually without using JET, that way you have more control over how the file is read and how your data structures are populated. You can build a proper CSV parser library without ever involving JET or as Mark pointed out using an existing library that doesn't use JET. I believe CSV is the simplest file format that you can easily manipulate using String/StringBuilder classes, arrays, Lists etc...

Cheers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900