View Full Version : Creating an access database in code


smudge
09-15-2006, 11:18 AM
I haven't used VB since version 4 and it was fairly easy to create an access database in code.

I want to get in to .NET development and bought a copy of VS2003 to play with. VB is totally different and it's confusing me something awful. Is there a way of creating an access database and adding tables in code?

I've looked in the help files, tried doing google searches but I don't seem to be able to find anything.

Thanks

Carl

skea
10-02-2006, 10:51 PM
Use ADOX Library. Attached is the dll and below is some sample code.
Hope it helps.

Imports System.Runtime.InteropServices
Imports ADOX
Public Class Form1
'Shows how to create an Access database and append tables, fields, indexes using ADOX. Don't forget
'a reference to ADOX (Microsoft ADO Ext. 2.x for DDL and Security)
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Dim ADOXcatalog As New Catalog
Dim ADOXtable As New Table
Dim myConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "c:\newdata.mdb"
Try
ADOXcatalog.Create(myConn)
'name table, append fields to table
ADOXtable.Name = "tblCustomers"
ADOXtable.Columns.Append("First_Name", ADOX.DataTypeEnum.adVarWChar, 40)
ADOXtable.Columns.Append("customerID", ADOX.DataTypeEnum.adInteger)
ADOXtable.Columns.Append("Address", ADOX.DataTypeEnum.adVarWChar, 20)
'append tables to database
ADOXcatalog.Tables.Append(ADOXtable)
Catch ex As Exception
MessageBox.Show("Error in btnCreate_Click():" & ex.Message)
Finally
ADOXtable = Nothing
ADOXcatalog = Nothing
End Try
End Sub

End Class

smudge
10-03-2006, 06:35 AM
Cool. Thanks :D