Create Access Database in VB.Net

BusterG

Member
Joined
Jan 11, 2003
Messages
15
Location
United Kingdom
I am writing an application in which I wish to use an Access database. However, if the database doesnt exist, I want to programatically re-create the database.

I have done this in the past using VB6 but as I am now using .Net have been investigating ADO.Net. The problem is that all the examples/resources I have found only demonstrate database creation using the SQL Server which I dont want to use.

Does this mean the only option is to forget ADO.Net and use COM instead?
 
I dont know how to create an Access database, only SQL Server. You can use ADOX as Thinker demos in this sample...http://www.visualbasicforum.com/showthread.php?s=&threadid=20002

Once you get the database created, heres how to create a table using vb.net...
Code:
        Try
            Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\test.mdb")
            con.Open()

            Dim cmd As New OleDbCommand("CREATE TABLE " & TextBox1.Text.ToString & " (Col1  Text, col2 Text)", con)
            cmd.ExecuteNonQuery()
            con.Close()

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
 
Thanks for the code sample and link but its not what im looking for. I am already using OleDbCommand methods to populate a DataGrid with the contents of a database but its the database creation thats the problem.

Also, I want to clarify, in many of the forum msgs I have seen, they say "shouldnt use COM use ADO.Net instead" but without any explanation. Does anyone know why? and what the difference is?

http://www.computerhelp.forum/t69182.html

I know OleDbConnect / OleDbCommand are obviously .Net methods but why is COM not a good idea? Not that I want to use it anyway.
 
Last edited by a moderator:
The code above does not populate a Datagrid, it creates tables
from scratch.

I dont know how to create a database using ADO.NET, thats
why I provided the Com solution for now.

Why not use COM/ActiveX/DLL? ... DOT NET was designed to do
away with it all and only use managed code. Im sure someone
else will elaborate.
 
I didnt mean to imply (if I did) that your code sample would populate a DataGrid. I thought I stated that I have been using similar OleDb methods to the code sample but want to achieve this (if possible) using ADO.Net.

However, what in the code indicates that this is a COM method? Is it the PROVIDER= parth of the connection string? I am confused because ADO does mean ActiveX Data Objects and as you said .Net was designed to lessen the necessity to use ActiveX so where is the boundry between what is COM/ActiveX and .Net?

In VB6 I wrote a class that used the Winsock control so I understand that even though I may only use a single method from that class, the class uses ActiveX to do its thing and so indirectly the method I call is using ActiveX.

How can I tell when a .Net method is using COM/ActiveX or arent they?
 
ADO.NET does not use COM/ActiveX its a class library. Anytime
you Import(s) System.Data.OleDb or SqlClient you are using
ADO.NET. In order to use an ActiveX component you must Add
Reference into the project. Which hopefully you have not done. :)

The connection string in both ADOs can be the same, the string
indicates which version of JET to use. (in the case of Accesss).

The Create Table statement uses the OleDbCommand, which is
ADO.NET, however you can use the exact same statement in VB6
using ADO COM.

Now to create a database.... I can do it using SqlClient but not
OleDB, Im sure its possible though, and maybe someone will
eventually point it out. But for now the only way I know is to use
ADOX (COM) which is spelled out in Thinkers example (in the link)

Sorry I couldnt be more help.
 
Many thanks. That has cleared up many questions I had with regards to other postings I have read.

I havent added any references to my project and have only used the OleDb class.

Again, thanks for your help. :)
 
Back
Top