- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming
C# program that interfaces with MS Access: Create a new Access database file
1. Open a new Visual C# .NET windows application. Name the project CreateDatabase.
2. In Solution Explorer, right-click the References node and select Add Reference. On the COM tab, select “Microsoft ADO Ext. 2.8 for DDL and Security”, and then click OK.
3. Design a form. Add a Textbox and a Button
4. Set Name property of Textbox to “txtdatabaseName”.
5. Set Name property of Button to “btnCreateDB” and text property to “Create Database”.
6. In the code behind file import the library Microsoft ADO Ext. 2.8 for DDL and Security using: using ADOX;
7. Double click on the Button and pest the following code into the “btnCreateDB_Click” event
CatalogClass cat = new CatalogClass();
try
{
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + txtdatabaseName.Text + ".mdb;" +
"Jet OLEDB:Engine Type=5"); // Create databse
MessageBox.Show("Database created successfully."); // Show confirmation to user
txtdatabaseName.Text = ""; // Clear input box
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
cat = null;
}
8. Press F5 to build and run the project.
9. Put your database name with location like c:\MyAccessDB in the text box and then press the “Create Database” Button. An access database by the name MyAccessDB.mdb will be created at the specified location.