G
Guest
Hi,
I want to write an in memory DataTable to a CSV file. Just to prove I can
get it working I created a DataTable with a single column called "IntColumn"
(containing a couple of rows of data) and ran the the following code :
public static void WriteToCSVFile( System.Data.DataTable dataTable,
string csvPath ) {
OleDbConnection connection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" +
csvPath + ";" +
"Extended
Properties=\"Text;HDR=Yes\"" );
connection.Open();
try {
// Create a CSV file with appropriate column headers
// OleDbCommand command = new OleDbCommand( "CREATE TABLE
[TestDataTable]([IntColumn] INT);", connection, null );
// command.ExecuteNonQuery();
// Populate the CSV file with all data from the dataTable
OleDbDataAdapter adapter = new OleDbDataAdapter( "SELECT * INTO ["
+ dataTable.TableName + "]", connection );
OleDbCommandBuilder builder = new OleDbCommandBuilder( adapter );
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
adapter.InsertCommand = builder.GetInsertCommand();
adapter.Update( dataTable );
} finally {
connection.Close();
}
}
But get the following error :
System.InvalidOperationException : Dynamic SQL generation is not supported
against a SelectCommand that does not return any base table information.
I tried creating the CSV file by uncommenting the lines above but I get the
following error (which is understandable) :
System.Data.OleDb.OleDbException : Cannot modify the design of table
'TestDataTable'. It is in a read-only database.
I have found the following code in an old posting to
microsoft.public.vb.general which writes an access table to a CSV file using
ADO (so I presume it should be possible to do a similar thing with ADO.Net) :
Sub ExportAccessToTextADO()
Dim cnn As New ADODB.Connection
Dim sqlString As String
cnn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=d:\My Documents\DB1.mdb;" & _
"Jet OLEDB:Engine Type=4;"
sqlString = "SELECT * INTO [Text;DATABASE=d:\My
Documents\TextFiles].[Plot.csv] FROM [tblPlot]"
cnn.Execute sqlString
cnn.Close
Set cnn = Nothing
End Sub
Any help much appreciated.
Thanks in advance,
Joel Gordon.
I want to write an in memory DataTable to a CSV file. Just to prove I can
get it working I created a DataTable with a single column called "IntColumn"
(containing a couple of rows of data) and ran the the following code :
public static void WriteToCSVFile( System.Data.DataTable dataTable,
string csvPath ) {
OleDbConnection connection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" +
csvPath + ";" +
"Extended
Properties=\"Text;HDR=Yes\"" );
connection.Open();
try {
// Create a CSV file with appropriate column headers
// OleDbCommand command = new OleDbCommand( "CREATE TABLE
[TestDataTable]([IntColumn] INT);", connection, null );
// command.ExecuteNonQuery();
// Populate the CSV file with all data from the dataTable
OleDbDataAdapter adapter = new OleDbDataAdapter( "SELECT * INTO ["
+ dataTable.TableName + "]", connection );
OleDbCommandBuilder builder = new OleDbCommandBuilder( adapter );
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
adapter.InsertCommand = builder.GetInsertCommand();
adapter.Update( dataTable );
} finally {
connection.Close();
}
}
But get the following error :
System.InvalidOperationException : Dynamic SQL generation is not supported
against a SelectCommand that does not return any base table information.
I tried creating the CSV file by uncommenting the lines above but I get the
following error (which is understandable) :
System.Data.OleDb.OleDbException : Cannot modify the design of table
'TestDataTable'. It is in a read-only database.
I have found the following code in an old posting to
microsoft.public.vb.general which writes an access table to a CSV file using
ADO (so I presume it should be possible to do a similar thing with ADO.Net) :
Sub ExportAccessToTextADO()
Dim cnn As New ADODB.Connection
Dim sqlString As String
cnn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=d:\My Documents\DB1.mdb;" & _
"Jet OLEDB:Engine Type=4;"
sqlString = "SELECT * INTO [Text;DATABASE=d:\My
Documents\TextFiles].[Plot.csv] FROM [tblPlot]"
cnn.Execute sqlString
cnn.Close
Set cnn = Nothing
End Sub
Any help much appreciated.
Thanks in advance,
Joel Gordon.