Creating a New Access Database

Problem

You need to create a new Microsoft Access database.

Solution

Use ActiveX Database Objects Extensions (ADOX) from .NET through COM interop.

You'll need a reference to Microsoft ADO Ext. 2.7 for DDL and Security from the COM tab in Visual Studio .NET's Add Reference Dialog.

The sample code contains an event handler and a single method:

Button.Click

Allows the user to specify the filename for the new Access database and then calls the CreateAccessDatabase( ) method in the sample to create the database.

CreateAccessDatabase( )

This method uses ADOX through COM interop to create the new Access database having the specified filename.

The C# code is shown in Example 10-6.

Example 10-6. File: CreateAccessDatabaseForm.cs

// Namespaces, variables, and constants
using System;
using System.Windows.Forms;

// . . . 

private void createButton_Click(object sender, System.EventArgs e)
{
 // Create the save file dialog object.
 SaveFileDialog sfd = new SaveFileDialog( );
 sfd.InitialDirectory = System.IO.Path.GetTempPath( );
 // Set the filter for Access databases.
 sfd.Filter = "Microsoft Access (*.mdb)*.mdb";

 // Open the dialog.
 if (sfd.ShowDialog( ) == DialogResult.OK)
 {
 // Of OK selected, create the Access database.
 String fileName = sfd.FileName;

 try
 {
 CreateAccessDatabase(fileName);

 MessageBox.Show("Microsoft Access database " +
 fileName + " created.", "Create Access Database",
 MessageBoxButtons.OK,
 MessageBoxIcon.Information);
 }
 catch (System.Exception ex)
 {
 MessageBox.Show("Could not create database " +
 fileName + ". " + ex.Message,
 "Create Access Database",
 MessageBoxButtons.OK, MessageBoxIcon.Error);
 }
 }
}

private void CreateAccessDatabase(String fileName)
{
 String connectString =
 @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
 fileName + ";";
 
 // Use ADOX to create the Access database.
 ADOX.Catalog cat = new ADOX.Catalog( );
 try
 {
 cat.Create(connectString);
 }
 finally
 {
 cat = null;
 }
}

Discussion

ADO Extensions for DDL and Security (ADOX) extends the ADO objects and programming model with objects for schema creation and modification, and for security. ADOX is used to programmatically access and manipulate the objects in a database.

You can use ADOX from .NET through COM interop to create a new Microsoft Access database. Use the Create( ) method of the ADOX.Catalog object, passing a connection string for the new Access database as the argument.

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax



ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net