You are currently offline, waiting for your internet to reconnect

How to create an Access database by using ADOX and Visual C# .NET

This article was previously published under Q317881
Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base:
840667 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application
For a Microsoft Visual Basic .NET version of this article, see 317867.

IN THIS TASK

SUMMARY
Programmers often need to create databases programmatically. Although neither ActiveX Data Objects (ADO) nor ADO.NET provide the means to create Microsoft Access databases automatically, you can use the Microsoft Jet OLE DB Provider and Microsoft ADO Ext. 2.7 for DDL and Security (ADOX) through the COM Interop layer to create databases manually.

Requirements

  • Microsoft Visual C# .NET
  • Microsoft ADO Ext. 2.7 for DDL and Security (ADOX)
This step-by-step article describes how to use ADOX and Visual C# .NET to create an Access database manually.


Build an Access Database

  1. Open a new Visual C# .NET console application.
  2. In Solution Explorer, right-click the References node and select Add Reference.
  3. On the COM tab, select Microsoft ADO Ext. 2.7 for DDL and Security, click Select to add it to the Selected Components, and then click OK.
  4. Delete all of the code from the code window for Class1.cs.
  5. Paste the following code into the code window:
    using System;using ADOX;namespace ConsoleApplication1{	class Class1	{		[STAThread]		static void Main(string[] args)		{			ADOX.CatalogClass cat = new ADOX.CatalogClass();			cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +				   "Data Source=D:\\AccessDB\\NewMDB.mdb;" +				   "Jet OLEDB:Engine Type=5");			Console.WriteLine("Database Created Successfully");			cat = null;		}	}}					
  6. Change the path to the new .mdb file as appropriate, and then press F5 to build and run the project.

    The new .mdb file will be created in Access 2000 (Jet 4.0) format. For more information about different Jet formats, see the "References" section of this article.

Pitfalls

To create the new database, the Jet Provider requires the path to exist. If you try to create a database file in a directory that does not exist, you will receivean exception error. You can catch this exception by using a Try...Catch structure.

REFERENCES

REFERENCES


For more information about the .NET Framework and the COM Interop layer, visit the following MSDN Web site:
Exposing COM Components to the .NET Framework
http://msdn.microsoft.com/en-us/library/z6tx9dw3(vs.71).aspx
ADOX C#.NET C#
Properties

Article ID: 317881 - Last Review: 05/16/2007 02:17:55 - Revision: 4.4

Microsoft ADO.NET 1.0, Microsoft ADO.NET 1.1, Microsoft Visual C# .NET 2002 Standard Edition, Microsoft Visual C# .NET 2003 Standard Edition

  • kbhowto KB317881
Feedback
="display:none;" onerror="var m=document.createElement('meta');m.name='ms.dqp0';m.content='true';document.getElementsByTagName('head')[0].appendChild(m);" onload="var m=document.createElement('meta');m.name='ms.dqp0';m.content='false';document.getElementsByTagName('head')[0].appendChild(m);" src="http://c1.microsoft.com/c.gif?">