470,590 Members | 2,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,590 developers. It's quick & easy.

convert Csv or text file to Access table using c#

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.

Nov 16 '05 #1
1 4143
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" <yh*******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by jj | last post: by
3 posts views Thread by Dave G | last post: by
15 posts views Thread by pakerly | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.