This application helps you to migrate any legacy Microsoft Access database to Excel or any other database, use the application as is or learn how to customize as per your need.
Introduction
Recently, I had a client who was using a legacy application created in Visual Basic with Microsoft Access and wanted to migrate the database to support newer Cloud based web applications.
The whole idea is to use the new application with the original data.
Background
The initial requirement was to have all the data exported to a format which can then be exported to any database. It was a one time process and the old system was meant to be deprecated once the new Cloud based web application is ready. We proposed a console application which will export the data to Excel/CSV which can then be used to import data into any database.
Using the Code
It's a small code where it follows the below steps to migrate the data:
- Connect to Access database and get list of tables.
- Loop through the tables and export each of the tables to a separate worksheet.
- Transforms data cell by cell, here you could add validations or transform the data.
Libraries used:
- Microsoft.Office.Interop.Excel: To simplify access to Office API objects
System.Data.OleDb
: .NET Framework Data Provider for OLE DB to connect to Access DB, but you can legacy database using this
DataSet ds = GetAllDataFromSource();
ExportDataSetToExcel(ds);
Step 1
Get all data from the source database:
private static DataSet GetAllDataFromSource()
{
System.Data.DataTable userTables = null;
OleDbDataAdapter oledbAdapter;
DataSet ds = new DataSet();
List<string> tableNames = new List<string>();
using (OleDbConnection myConnection = new OleDbConnection())
{
myConnection.ConnectionString = ConfigurationManager.ConnectionStrings
["SourceDatabaseConnectionString"].ConnectionString;
myConnection.Open();
string[] restrictions = new string[4];
restrictions[3] = "Table";
userTables = myConnection.GetSchema("Tables", restrictions);
for (int i = 0; i < userTables.Rows.Count; i++)
{
var tableName = userTables.Rows[i][2].ToString();
oledbAdapter = new OleDbDataAdapter($"select * from {tableName}", myConnection);
oledbAdapter.Fill(ds, $"{tableName}");
if (ds.Tables[$"{tableName}"].Rows.Count > 0)
{
Console.WriteLine("Rows: " + ds.Tables[$"{tableName}"].Rows.Count);
}
oledbAdapter.Dispose();
}
myConnection.Close();
}
return ds;
}
Step 2
Export the data to Excel:
private static void ExportDataSetToExcel(DataSet ds)
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbook excelWorkBook =
excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
foreach (System.Data.DataTable table in ds.Tables)
{
Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add();
excelWorkSheet.Name = table.TableName;
for (int i = 1; i < table.Columns.Count + 1; i++)
{
excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
}
for (int j = 0; j < table.Rows.Count; j++)
{
for (int k = 0; k < table.Columns.Count; k++)
{
try
{
excelWorkSheet.Cells[j + 2, k + 1] =
table.Rows[j].ItemArray[k].ToString();
}
catch(Exception ex)
{
Console.WriteLine($"Error in table:
{excelWorkSheet.Name} - Cells - j: {j},
k:{k}, data: {table.Rows[j].ItemArray[k].ToString()}");
Console.WriteLine(ex);
}
}
}
}
string fileName = System.IO.Path.Combine
(System.Configuration.ConfigurationManager.AppSettings
["TargetDirectory"], $@"test-{DateTime.Now.ToString
("yyyyMMddHHmmss")}.xls");
excelWorkBook.SaveAs(fileName);
excelWorkBook.Close();
excelApp.Quit();
}
Results:
Feel free to modify/extend this code on Github at https://github.com/rohitsies/DataExportFromMSAccess/blob/master/README.md.
Points of Interest
Microsoft.Office.Interop
is a great tool to interact with Excel and other Office applications. It provides seamless integration, saves time and efforts.
History
- 1st June, 2020: Initial article posted
- 2nd June, 2020: Formatting corrected