Solved

Exporting Ms Access data to CSV using C#

Posted on 2008-06-14
7
258 Views
Last Modified: 2010-04-28
Hi I am trying to export ms access data to CSV using C#. I have a problem with capturing its foreign key enforcement and check constraints. I can find codes for exporting csv to access but the otherwise. I am new here. Can anyone kindly share with me? Thanks
0
Comment
Question by:Jaded25
  • 3
  • 2
7 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 21788484
Hi,
why don't you just create a view on Access with the data and schema you want to export to CSV and then just make a loop on C# that concatenates the cell values of each row separated by ';'  ?

Each loop would do a WriteLine to the StreamWriter of the concatenated strings... that's it... :)
0
 

Author Comment

by:Jaded25
ID: 21788555
Thanks for your help. Would it be alright if I want to know the code? Thanks :)
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 21788620
I was going to write it but decided to do a little google search first so here's a sample code of it:

http://dotnetguts.blogspot.com/2007/01/exporting-datatable-to-csv-file-format.html

I'm attaching the same code but better formatted and I changes the sparator from ',' to ';'
This is just because if you have numeric values and depending on your region the comma may be a problem.

cheers
using System;

using System.Data;

using System.IO;
 

namespace WorkBench

{

	public static class WriteCSVDemo

	{
 

		public static void  CreateCSVFile(DataTable dt, string strFilePath)

		{
 
 

			#region Export Grid to CSV
 

			// Create the CSV file to which grid data will be exported.

			StreamWriter sw = new StreamWriter(strFilePath, false);
 

			// First we will write the headers.

			int iColCount = dt.Columns.Count;
 

			for (int i = 0; i < iColCount; i++)

			{

				sw.Write(dt.Columns[i]);

				if (i < iColCount - 1)

				{

					sw.Write(";");

				}

			}
 

			sw.Write(sw.NewLine);
 

			// Now write all the rows.

			foreach (DataRow dr in dt.Rows)

			{

				for (int i = 0; i < iColCount; i++)

				{

					if (!Convert.IsDBNull(dr[i]))

					{

						sw.Write(dr[i].ToString());

					}

					if (i < iColCount - 1)

					{

						sw.Write(";");

					}

				}
 

				sw.Write(sw.NewLine);

			}
 

			sw.Close();
 

			#endregion
 

		}
 

	}

}

Open in new window

0
 

Author Comment

by:Jaded25
ID: 21788930
Thanks you so much for the help.
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 21788950
Glad I could help,

Cheers!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now