Solved

Read .CSV file

Posted on 2003-10-28
2
2,441 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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