Solved

Read .CSV file

Posted on 2003-10-28
2
2,427 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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now