Solved

Read .CSV file

Posted on 2003-10-28
2
2,438 Views
Last Modified: 2009-12-16
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
Comment
Question by:wazzajs
2 Comments
 
LVL 5

Accepted Solution

by:
tgannetts earned 190 total points
ID: 9641408
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
 
LVL 2

Author Comment

by:wazzajs
ID: 9727521
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hey Audio! 9 44
C# LINQ ForEach() question 6 54
Need a simple RegEx to search for two words 27 44
Adding items to a C# list incrementally 5 30
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question