【C#】【ACCESS】ユーザから選択されたACCESSファイル(.mdb)を使う

■ サンプル

 * ACCESSファイルのデータをCSVファイルに変換する

DataSet

namespace Tool001.Data.PersonDataSetTableAdapters
{
    public partial class PersonTableAdapter
    {
        public string ConnectionString
        {
            get { return this.Connection.ConnectionString; }
            set { this.Connection.ConnectionString = value; }
        }
    }
}

画面側

/// <summary>
/// TableAdapter
/// </summary>
private PesonTableAdapter personTableAdapter = null;

#region 「ファイルを取り込む」ボタン押下後
/// <summary>
/// 「ファイルを取り込む」ボタン押下後のイベント
/// </summary>
private void buttonOpenFile_Click(object sender, EventArgs e)
{
    try
    {
        // ファイルダイアログオープン
        DialogResult ret = this.openFileDialog1.ShowDialog();
        if (ret == DialogResult.OK)
        {
        	string connectString = string.Format(
        		"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}"
        		, this.openFileDialog1.FileName);
        	
        	this.testTableAdapter = new TestTableAdapter();
        	this.testTableAdapter.ConnectionString = connectString;
        	
        	PersonDataSet mdbDataSet = new PersonDataSet();
        	this.personTableAdapter.Fill(mdbDataSet.Test);
        	
        	using (StreamWriter sw = new StreamWriter(
        		"\\" // CSVファイル名
        		, true // 既存のテキスト内容がすべて残されたまま、その末尾にテキスト内容が追記
        		, Encoding.GetEncoding("Shift_JIS"))) // 文字コード
        	{
	        	foreach (PersonDataSet.TestRow row in mdbDataSet.Test)
	        	{
	        		bool isNotFirst = false; // 初めのデータではないかどうか
	        		for (int i = 0; i < 4; i++)
	        		{
	        			if (isNotFirst)
	        			{
	        				sw.Write(",");
	        			}
	        			
	        			string data = row[i].ToString();
	        			if (string.IsNullOrEmpty(data))
	        			{
	        				data = string.Empty;
	        			}
	        			sw.Write(data);
	        			
	        			isNotFirst = true;
	        		}
	        	}
        	}
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
#endregion

関連記事

ACCESS

ACCESSデータベースを使う
https://blogs.yahoo.co.jp/dk521123/19032377.html