C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to get Excel file's data into DataSet


Posted Date: 16 Apr 2008    Resource Type: Code Snippets    Category: C# Syntax

Posted By: Jessica       Member Level: Gold
Rating:     Points: 10



Hi All,
The below given function will read the the CSV file and make a dataset from that file's data.User have you specify the filename with path in parameter "filename" and if header is needed then provide "userHeader" = true. It uses the simple SELECT query to fetch the data from file and fill the data into dataset with OleDbDataAdapter .

Hope you will find it informative.

Regards.


public System.Data.DataSet getCSVData(string filename, bool useHeader)
{

string myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ System.IO.Path.GetDirectoryName(filename)
+ ";Extended Properties=\"text;";
if (useHeader)
{
myConnectionString += "HDR=YES;";
}
else
{
myConnectionString += "HDR=NO;";
}
myConnectionString += "FMT=Delimited(,);";
myConnectionString += "\"";
System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection();
myConnection.ConnectionString = myConnectionString;
myConnection.Open();
string myCommand = "SELECT * FROM " + System.IO.Path.GetFileName(filename);
System.IO.Path.GetFileName(filename).Replace(".", "#");
System.Data.OleDb.OleDbDataAdapter myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(myCommand, myConnection);
System.Data.DataSet myData = new System.Data.DataSet();
myDataAdapter.Fill(myData);
if (myConnection.State != System.Data.ConnectionState.Closed)
{
myConnection.Close();
}
return(myData);
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Excel to DataSet  .  CODE  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Autonumber appending for file saving
Previous Resource: Return all positions of a string in a string
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use