
Knowledge Base
HOW TO: Create a Microsoft Access Database Using ADOX and Visual Basic .NETPSS ID Number: 317867
Article Last Modified on 2/8/2003
--------------------------------------------------------------------------------
The information in this article applies to:
Microsoft ADO.NET (included with the .NET Framework)
Microsoft Visual Basic .NET (2002)
--------------------------------------------------------------------------------
This article was previously published under Q317867
For a Microsoft Visual C# .NET version of this article, see 317881.
IN THIS TASK
SUMMARY
Requirements
Steps to Build Example
Troubleshooting
REFERENCES
SUMMARY
Programmers may have to create databases programmatically, but neither ActiveX Data Objects (ADO) nor ADO.NET provides the means to create Microsoft Access databases. However, you can create Access databases by using the Microsoft Jet OLE DB Provider and Microsoft ADO Ext. 2.7 for DDL and Security (ADOX) with the COM Interop layer.
Requirements
Microsoft Visual Basic .NET
ADO Ext. 2.7 for DDL and Security (ADOX)
This step-by-step example describes how to use ADOX and Visual Basic .NET to create an Access database on the fly.
back to the top
Steps to Build Example
Open a new Visual Basic .NET Console application.
In Solution Explorer, right-click the References node, and then click Add Reference.
In the Add Reference dialog box, click the COM tab, click Microsoft ADO Ext. 2.7 for DDL and Security, click Select to add it to the Selected Components section, and then click OK.
Delete all of the code from the code window for Module1.vb.
Copy the following code and paste it in the code window:
Imports ADOX
Module Module1
Sub Main()
Dim cat As Catalog = New Catalog()
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 = Nothing
End Sub
End Module
Change the path to the new .mdb file as appropriate. Make sure the folder provided in the path exists. Press F5 to build and run the project.
The new .mdb file is created in Access 2000 (Jet 4.0) format. For a different Jet format, see the "References" section of this article.
back to the top
Troubleshooting
The Jet Provider requires that the path exists to create the new database. If you try to create a database file in a path that does not exist, you receive an exception. This exception can be caught by using a try...catch structure.
back to the top
REFERENCES
For additional information about how to create a table with a primary key through ADOX, click the following article number to view the article in the Microsoft Knowledge Base:
252908 HOWTO: Create a Table with Primary Key Through ADOX
For more details about the .NET Framework and the COM Interop layer, visit the following Microsoft Web site:
Exposing COM Components to the .NET Framework
http://msdn.microsoft.com/library/defau ... mework.asp
For more details about Microsoft Jet 4.0 Engine Type values, visit the following Microsoft Web site:
Appendix C: Microsoft Jet 4.0 OLE DB Provider-Defined Property Values
http://msdn.microsoft.com/library/defau ... opic15.asp
back to the top
Additional query words: ADOX VB.NET
Keywords: kbHOWTOmaster KB317867
Technology: kbADONETSearch kbADOsearch kbAudDeveloper kbVBNET kbVBNETSearch kbVBSearch
--------------------------------------------------------------------------------
Send feedback to Microsoft
© 2003 Microsoft Corporation. All rights reserved.
Friend Sub CreateDb(ByVal DB As String)
Dim myType As Type
Dim myObject As Object
Dim myDbObject As Object
Dim myDbType As Type
Dim arguments() As Object
'myType = Type.GetTypeFromProgID("DAO.DBEngine.36")
myType = Type.GetTypeFromProgID("ADOX.Catalog")
'// Получает тип нашего COM-объекта
myObject = Activator.CreateInstance(myType)
' Создаем сам объект
'arguments = New Object() {db, dbLangCyrillic(надо посмотреть значение) }
arguments = New Object() {Db}
'myDbObject = myType.InvokeMember("CreateDatabase", Reflection.BindingFlags.InvokeMethod, Nothing, myObject, arguments)
myDbObject = myType.InvokeMember("Create", Reflection.BindingFlags.InvokeMethod, Nothing, myObject, arguments)
myDbType = myDbObject.GetType
myType.InvokeMember("Close", Reflection.BindingFlags.InvokeMethod, Nothing, myDbObject, Nothing)
System.Runtime.InteropServices.Marshal.ReleaseComObject(myDbObject)
System.Runtime.InteropServices.Marshal.ReleaseComObject(myObject)
End Sub
Sebas писал(а):не кайфы, так надо ещё дллку таскать....
Дык приведенный код использует те-же DAO/ADOX.
Так что от них ты никуда не денешься -> не проще ли просто использовать ADOX как обычно - одной строчкой?
Кстати, MDAC, кажись, входит в Framework...
P.S
Кстати, если хочется использовать позднее связывание, то чем приведенный код лучше тривиального CreateObject? Смысл?
Sebas писал(а):ну, просто языко-независимая конструкция. Соответственно и быстрее работает.
Andrey Fedorov писал(а):Sebas писал(а):ну, просто языко-независимая конструкция. Соответственно и быстрее работает.
Не знаю... не знаю... Вряд разница в скорости создания базы превысит хотя бы долю процента...
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 7