I am trying to write a program which creates a .csv file from an Access table. I'm not sure how to do this and everything I've found while researching this is how to do the opposite, creating an Access table from a .csv file.
So far, I have created a windows forms application that allows the user to select a data path to the access directory (.mdb) and then after pushing the "Go" button, a list of the tables in the directory is shown in a listbox.
What I need to do next, which I have no idea how to, is allow the user to select one of the tables, which would then create a .csv file from the selected table. Here is my code so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Common;
namespace TranslatorHelper
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnPath_Click(object sender, EventArgs e)
{
string dialogText = "";
// Show the dialog and get result.
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
dialogText = openFileDialog1.ToString();
dialogText = dialogText.Replace("System.Windows.Forms.OpenFileDialog: Title: , FileName: ", "");
txtDataPath.Text = dialogText;
}
}
private void btnGetTables_Click(object sender, EventArgs e)
{
// Microsoft Access provider factory
DbProviderFactory factory =
DbProviderFactories.GetFactory("System.Data.OleDb");
DataTable userTables = null;
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+txtDataPath.Text;
// We only want user tables, not system tables
string[] restrictions = new string[4];
restrictions[3] = "Table";
connection.Open();
// Get list of user tables
userTables = connection.GetSchema("Tables", restrictions);
}
// Add list of table names to listBox
for (int i = 0; i < userTables.Rows.Count; i++)
lstTables.Items.Add(userTables.Rows[i][2].ToString());
}
private void lstTables_SelectedIndexChanged(object sender, EventArgs e)
{
}
Please offer any advice you can, anything is appreciated, I'm really stuck here.