convert Csv or text file to Access table using c#

J

JH

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.
 
K

Kai Brinkmann [MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top