Here is my code for converting an Access Database to CSV files using C#. Please see my original blog post for more info.
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using System.Data;
5: using System.Data.OleDb;
6: using System.Xml.Serialization;
7: using System.IO;
8:
9: namespace exportapp
10: {
11: class Program
12: {
13: static void Main(string[] args)
14: {
15: if (args.Length < 2)
16: {
17: Console.WriteLine("Usage ExportApp <input access file path> <output directory path>");
18: return;
19: }
20:
21: // TODO: validate input file path, use input file path for output directory if not specified
22:
23: string strAccessConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
24: strAccessConnection += args[0];
25:
26: OleDbConnection connection = null;
27: try
28: {
29: connection = new OleDbConnection( strAccessConnection );
30: }
31: catch( Exception ex )
32: {
33: Console.WriteLine( "Error: Failed to create a database connection. \n{0}", ex.Message );
34: return;
35: }
36:
37: try
38: {
39: connection.Open();
40: DoSQLToFile(connection, "groups", args[1]);
41: DoSQLToFile(connection, "products", args[1]);
42: DoSQLToFile(connection, "[group-product]", args[1]);
43: DoSQLToFile(connection, "songs", args[1]);
44: }
45: finally
46: {
47: connection.Close();
48: }
49: }
50:
51: static void DoSQLToFile(OleDbConnection conn, string tablename, string directory)
52: {
53: string selectString = "SELECT * FROM " + tablename;
54: string path = directory + "\\" + tablename + ".csv";
55: TextWriter tw = new StreamWriter(path);
56:
57: DataSet myDataSet = new DataSet();
58: try
59: {
60: OleDbCommand command = new OleDbCommand(selectString, conn);
61: OleDbDataAdapter adapter = new OleDbDataAdapter(command);
62:
63: adapter.Fill(myDataSet, tablename);
64: }
65: catch (Exception ex)
66: {
67: Console.WriteLine("Error: Failed to retrieve the required data from the Database.\n{0}", ex.Message);
68: throw ex;
69: }
70: DataTableCollection dta = myDataSet.Tables;
71: foreach (DataTable dt in dta)
72: {
73: Console.WriteLine("found data table {0}", dt.TableName);
74: }
75:
76: // The column info is automatically fetched from the database,
77: // so we can read it here:
78: Console.WriteLine("{0} columns in {1} table", myDataSet.Tables[0].Columns.Count, myDataSet.Tables[0].TableName);
79:
80: // write out the headers
81: DataColumnCollection drc = myDataSet.Tables[0].Columns;
82: string headerString = "";
83: bool writeComma = false;
84: foreach (DataColumn dc in drc)
85: {
86: if (writeComma)
87: {
88: headerString += ", ";
89: }
90: else
91: {
92: writeComma = true;
93: }
94: headerString += "\"" + dc.ColumnName + "\"";
95: }
96: tw.WriteLine(headerString);
97: DataRowCollection dra = myDataSet.Tables[0].Rows;
98: foreach (DataRow dr in dra)
99: {
100: string rowString = "";
101: writeComma = false;
102: foreach (DataColumn dc in myDataSet.Tables[0].Columns)
103: {
104: if (writeComma)
105: {
106: rowString += ", ";
107: }
108: else
109: {
110: writeComma = true;
111: }
112: rowString += "\"" + dr[dc] + "\"";
113: }
114: tw.WriteLine(rowString);
115: }
116: tw.Close();
117: }
118: }
119: }