Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2446
  • Last Modified:

Read .CSV file

Hi,

I was wondering if anyone could help me.

i have a csv file which is located locally at c:\INPUT.CSV

i am trying to read this file into a Datagrid but with out success.

i can do it if it was a Access database using something like

System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\WSFEEDBACK.mdb");
                                    
aConnection.Open();      
OleDbCommand aCommand = new OleDbCommand("SELECT * FROM WebEvents ",aConnection);
DataSet DS = new DataSet();
                                          
try
{
OleDbDataAdapter myDA = new OleDbDataAdapter(aCommand);      
myDA.Fill(DS);
OleDbDataReader myDataReader = aCommand.ExecuteReader();
                        
 if( myDataReader.Read() )
      {
And so on..............................

Bit HOW do i fill a Dataset if it was a CSV file?

any help will be much appreciated..

Thanks.

wazzajs

0
wazzajs
Asked:
wazzajs
1 Solution
 
tgannettsCommented:
I use the following to create a dataset from a csv:
(using your 'c:\Input.csv' as example)

using System;
using System.Data;
using System.Data.OleDb;

...

DataSet ds;
string sCSVFile = "Input.csv";
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\;Extended Properties='text;HDR=No;FMT=Delimited'";
OleDbConnection conn = new OleDbConnection(sConnectionString);
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM ["+ sCSVFile + "]", conn);

try
{
conn.Open();
ds = new DataSet("MyDataSet");
da.FillSchema(ds, SchemaType.Source, sCSVFile);
da.Fill(ds, sCSVFile);

... //Do something with dataset

conn.Close();

}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
return;
}

return;

Hope this helps.

Tom.
0
 
wazzajsAuthor Commented:
Sorry for the late reply, had to work on something else so never got the chance to check for response...

your solution works great!

thanks for your time...

wazzajs
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now