954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading CSV file into a ADO recordset

I'm relatively new to programming.

Problem:
I'm creating an ASP.NET app the needs to read a CSV file and filter the data.

I searched the net and only found classic ASP examples or all DB driven examples.

Does anyone have sample code. Or is there any other suggestions on how to read and filter. Thanks

sseo
Newbie Poster
2 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

c# code

using System.Data;

//in some function
System.Data.Odbc.OdbcConnection conn;
DataTable dt;
System.Data.Odbc.OdbcDataAdapter da;
string connectionString;
string importFolder;
string fileName;

importFolder = @"c:\importfile" //or pass it in -this is the folder in which the csv file is in

fileName = "csvimport.csv" //or pass it in -this is the csv file to be imported

connectionString= @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + importFolder + ";";
conn = new Odbc.OdbcConnection(connectionString)

//we only pass it the folder.  The csv file import is in the query which follows

da = new System.data.Odbc.OdbcDataAdapter("select * from [" + strFileName + "]", conn);
da.Fill(dt);

//Your table is filled and uses the first row of the csv file as the column headings.


Hope it helps :)

f1 fan
Posting Whiz in Training
279 posts since Jan 2006
Reputation Points: 26
Solved Threads: 11
 

Thanks for the code f1_fan.

I'll try to work with the code.

Does anyone have a VB example as well.

sseo
Newbie Poster
2 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

I will change it to vb but bear with me as my vb is rusty to say the least.

imports System.Data;

//in some function
Dim conn As System.Data.Odbc.OdbcConnection 
Dim dt As DataTable
Dim da As System.Data.Odbc.OdbcDataAdapter;
Dim connectionString As string
Dim importFolder As string
Dim fileName As string

importFolder = "c:\importfile" //or pass it in -this is the folder in which the csv file is in

fileName = "csvimport.csv" //or pass it in -this is the csv file to be imported

connectionString= "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + importFolder + ";"
conn = new Odbc.OdbcConnection(connectionString)

//we only pass it the folder.  The csv file import is in the query which follows

da = new System.data.Odbc.OdbcDataAdapter("select * from [" + strFileName + "]", conn);
da.Fill(dt);

//Your table is filled and uses the first row of the csv file as the column headings.
f1 fan
Posting Whiz in Training
279 posts since Jan 2006
Reputation Points: 26
Solved Threads: 11
 

Can I access a remote csv? Example: http://www.domain.com/file.csv

Romulinho
Newbie Poster
1 post since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

Thank you for the code, it really helped me
:)
c# code

using System.Data;

//in some function
System.Data.Odbc.OdbcConnection conn;
DataTable dt;
System.Data.Odbc.OdbcDataAdapter da;
string connectionString;
string importFolder;
string fileName;

importFolder = @"c:\importfile" //or pass it in -this is the folder in which the csv file is in

fileName = "csvimport.csv" //or pass it in -this is the csv file to be imported

connectionString= @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + importFolder + ";";
conn = new Odbc.OdbcConnection(connectionString)

//we only pass it the folder.  The csv file import is in the query which follows

da = new System.data.Odbc.OdbcDataAdapter("select * from [" + strFileName + "]", conn);
da.Fill(dt);

//Your table is filled and uses the first row of the csv file as the column headings.


Hope it helps :)

wakamohle
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

You're welcome.

I'm glad you found it useful. Please read member rules - http://www.daniweb.com/forums/faq.php?faq=daniweb_policies

Thread Closed.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You