Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert Csv or text file to Access table using c#

57 views
Skip to first unread message

JH

unread,
Nov 16, 2004, 5:45:20 PM11/16/04
to
I have a comma delimited file and I want to export it to an MS access table
already designed with appropriate field names. How do I do this
programmatically using VB.NET or C#?

Thanks for any help in advance.

Kai Brinkmann [MSFT]

unread,
Nov 16, 2004, 6:59:54 PM11/16/04
to
Create an OleDbConnection and an OleDbDataAdapter with the appropriate
queries. Create a dataset and use the adapters Fill() method. Parse the CSV
file (you can use the Split() method to separate the values in each line).
Create one new DataRow for each line and assign the values from the CSV file
to the appropriate colums, then add each row to the DataSet. Finally, call
the data adapter's Update() method to write the modified data back to the
underlying DB.

In C# it looks something like this:

OleDbConnection myConn = new OleDbConnection(myConnection); // myConnection
is the connection string
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();

// now you need to define the appropriate queries for the adapter. You can
do this programmatically, by assigning queries
// the the adapter's command properties, but if you are using VS to add the
adapter to your form in design mode
// there are wizards available to design the queries

myConn.Open();

DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet);

// now modify the data set. The data set is an in-memory representation of a
relational DB. It's essentially a collection of
// DataTable objects (which ones it contains depends on your SELECT query).
You can access these objects using
// the familiar syntax. For example,
myDataSet.Tables["Customers"].Rows[0]["ID"].Value would give you access
// to the value stored in the ID column in the first row of the table named
Customers. You can use the tables NewRow()
// method to create a DataRow for this table, assign values to the columns
(these come from your CSV file) and add
// the DataRow to your table. Finally:

myDataAdapter.Update(myDataSet, tableName);

// writes your changes for table 'tableName' back to the underlying Access
DB.

myConn.Close();


I hope this helps.
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.


"JH" <yhco...@yahoo.com> wrote in message
news:%23Lz$Z6CzEH...@TK2MSFTNGP10.phx.gbl...

0 new messages