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); }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|