This article will show you a clear introduction of how to Export Data to MS Access in C# via a .NET Data Export component. Spire.DataExport for.NET is designed to help developers to perform data exporting processing tasks. With Spire.DataExport, the whole exporting process is quickly and it only needs two simple steps.
Please download Spire.DataExport for .NET and install it on your system, add Spire.DataExport.dll as reference in the downloaded Bin folder thought the below path: “…\Spire.DataExport\Bin\NET4.0\ Spire.DataExport.dll”.
Step 1: Load Data Information
In this step, Spire.DataExport helps us load data from database. Through DataGridVew, we can even preview and modify data.
private void Form1_Load(object sender, EventArgs e) { oleDbConnection1.ConnectionString = txtConnectString.Text; oleDbCommand1.CommandText = txtCommandText.Text; using (OleDbDataAdapter da = new OleDbDataAdapter()) { da.SelectCommand = oleDbCommand1; da.SelectCommand.Connection = oleDbConnection1; DataTable dt = new DataTable(); da.Fill(dt); dataGridView1.DataSource = dt; } }
Please check the screenshot:
Step 2: Set Export into MS Access
Here we need to set it as Access format. Spire.DataExport will create a new Access and through DataGridView export data into Access file. You can rename the file as you like.
private void btnExportToAccess_Click(object sender, EventArgs e) { Spire.DataExport.Access.AccessExport accessExport = new Spire.DataExport.Access.AccessExport(); accessExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable; accessExport.DataTable = this.dataGridView1.DataSource as DataTable; accessExport.DatabaseName = @"..\..\ToMdb.mdb"; accessExport.TableName = "ExportFromDatatable"; accessExport.SaveToFile(); }
Here comes to the results: