11.12. Creating a New Microsoft Access Database
Problem
You need to programmatically create a new Microsoft Access database.
Solution
Use ActiveX Database Objects Extensions for DDL and Security (ADOX) from .NET through COM Interop.
The solution calls the CreateAccessDatabase() method of the ADOX.Catalog class to create the Microsoft Access database.
The solution needs a reference to Microsoft ADO Ext. 2.8 (or later) for DDL and Security from the COM tab in Visual Studio .NET's Add Reference Dialog.
The C# code in Program.cs in the project CreateAccessDatabase is shown in Example 11-14.
Example 11-14. File: Program.cs for CreateAccessDatabase solution
using System;
namespace CreateAccessDatabase
{
class Program
{
private static string fileName = @"..\..\..\MyDatabase.accdb";
static void Main(string[] args)
{
string connectString =
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";";
// Use ADOX to create the Access database.
ADOX.Catalog cat = new ADOX.Catalog( );
cat.Create(connectString);
cat = null;
Console.WriteLine("Microsoft Access database {0} created.", fileName);
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey( );
}
}
}The output is shown in Figure 11-13.

Figure 11-13. Output for CreateAccessDatabase solution
Discussion
ADO Extensions for DDL and Security (ADOX) extends the ADO objects and pro-gramming model with objects for schema creation and modification, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access