J
Joel Gordon
Hi,
I am reading a CSV file into a DataTable with the following code :
internal static DataTable ReadFromCSVFile( string csvFileName ) {
if ( !File.Exists( csvFileName ) ) {
throw new ApplicationException( " Cannot find CSV file : \"" +
csvFileName + "\"." );
}
string csvPath = Path.GetDirectoryName( csvFileName );
OleDbConnection connection =
new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + csvPath + ";" +
"Extended Properties=\"Text;HDR=Yes\"" );
return ReadDataTable( connection, Path.GetFileName(csvFileName) );
}
private static DataTable ReadDataTable( OleDbConnection connection,
string tableName ) {
connection.Open();
try {
// Read all data into a data table.
OleDbDataAdapter adapter =
new OleDbDataAdapter( "SELECT * FROM [" + tableName + "]",
connection );
DataTable dataTable = new System.Data.DataTable( tableName );
adapter.Fill( dataTable );
return dataTable;
} finally {
connection.Close();
}
}
How do I specify the format of columns containing DateTime values ?
If my CSV file contains the following :
Ints,Doubles,Dates1,Dates2
34,34.5,01/02/2003,12:43:00 p.m.,01/02/2003
Then the "Dates2" column in the DataTables is of type DateTime, but the
"Dates1" column is of type string.
Thanks in advance,
Joel Gordon.
I am reading a CSV file into a DataTable with the following code :
internal static DataTable ReadFromCSVFile( string csvFileName ) {
if ( !File.Exists( csvFileName ) ) {
throw new ApplicationException( " Cannot find CSV file : \"" +
csvFileName + "\"." );
}
string csvPath = Path.GetDirectoryName( csvFileName );
OleDbConnection connection =
new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + csvPath + ";" +
"Extended Properties=\"Text;HDR=Yes\"" );
return ReadDataTable( connection, Path.GetFileName(csvFileName) );
}
private static DataTable ReadDataTable( OleDbConnection connection,
string tableName ) {
connection.Open();
try {
// Read all data into a data table.
OleDbDataAdapter adapter =
new OleDbDataAdapter( "SELECT * FROM [" + tableName + "]",
connection );
DataTable dataTable = new System.Data.DataTable( tableName );
adapter.Fill( dataTable );
return dataTable;
} finally {
connection.Close();
}
}
How do I specify the format of columns containing DateTime values ?
If my CSV file contains the following :
Ints,Doubles,Dates1,Dates2
34,34.5,01/02/2003,12:43:00 p.m.,01/02/2003
Then the "Dates2" column in the DataTables is of type DateTime, but the
"Dates1" column is of type string.
Thanks in advance,
Joel Gordon.