// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Data.OleDb; using System.Runtime.InteropServices; using Access; namespace E { public class Program { public static void ExportOldWay(string db, string tbl, string fnm) { Application ac = new ApplicationClass(); ac.OpenCurrentDatabase(db, false, ""); try { ac.DoCmd.TransferText(AcTextTransferType.acExportDelim, null, tbl, fnm, false, null, 1252); } finally { ac.CloseCurrentDatabase(); ac.DoCmd.Quit(AcQuitOption.acQuitSaveNone); Marshal.ReleaseComObject(ac); } } public static void ExportNewWay(string db, string tbl, string fnm) { using(OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + db)) { con.Open(); StreamWriter sw = new StreamWriter(fnm); OleDbCommand cmd = new OleDbCommand("SELECT * FROM " + tbl, con); OleDbDataReader rdr = cmd.ExecuteReader(); while(rdr.Read()) { for(int i = 0; i < rdr.FieldCount; i++) { if(i > 0) sw.Write(";"); if(rdr[i] is String) { sw.Write("\"" + rdr[i] + "\""); } else { sw.Write(rdr[i]); } } sw.WriteLine(); } rdr.Close(); sw.Close(); } } public static void Main(string[] args) { ExportOldWay(@"C:\Databases\MSAccess\Test.mdb", "T1", @"C:\z1.txt"); ExportNewWay(@"C:\Databases\MSAccess\Test.mdb", "T1", @"C:\z2.txt"); } } }