Click to See Complete Forum and Search --> : Read .csv File into DataGrid?


sonicsqwirl
July 18th, 2005, 08:27 AM
I'm pretty new to the C# stuff. Can anyone help me with the code to read a .CSV file delimited by ","? The data file output is 14 columns with a dynamic number of rows (number of rows depends on the day).

Thanks in Advance,
sonicsqwirl

exterminator
July 18th, 2005, 09:09 AM
There is a small tutorial with code at this link (http://www.heikniemi.net/hc/archives/000152.html). Here some alternative code to do this. But I havent tested it as yet, so better test it first before using it.
static DataSet ReadCSV(string file)
{
if (! File.Exists(file) )
return null;
DataSet ds = new DataSet();
OleDbConnection conn = null;
string FileConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
@Path.GetDirectoryName(file) + ";" + @"Extended Properties=""Text;HDR=YES;""";
try
{
try
{
conn = new OleDbConnection();
conn.ConnectionString = FileConnection;
conn.Open();
}
catch
{
Console.WriteLine("Database connection can't be established");
return null;
}
string sql = String.Format("SELECT * FROM {0}",Path.GetFileName(file));
OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn);
oda.SelectCommand.ExecuteNonQuery();
oda.Fill(ds, "TableName");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
finally
{
if(conn != null)
conn.Dispose();
}
return ds;
}
This is a link to tutorial (http://www.codeproject.com/aspnet/ImportExportCSV.asp) on how to export/import from csv using datasets. Hope this helps. And don't forget to rate good/helpful posts. :thumb:

indianj
November 20th, 2009, 12:04 PM
How do I take good data from CSV file and convert it back to .mdb file?

Theone2k
November 20th, 2009, 06:15 PM
Would you not connect to the MDB and run an Append Script?

take a look here, will show you how to connect to MDB

http://www.csharphelp.com/archives/archive70.html