1 Previous Next 

Access (OleDB): Create DataBase File with ADODB and the Tables with AdoNet


This tip creates an Jet (Access) database and some tables. In this sample is ADODB used for creating the database while ADONET is used for the rest

You need to set in advance a reference to COM adox ext 2.x for dll and security

Project->Add Reference -> COM -> Adox etc. 

Imports System.Windows.Forms
Module Module1
    ' You need to set in advance a reference to COM adox ext 2.x for dll and security
    ' And for console programs one to Net System.Windows.Forms
    'Project->Add Reference -> COM -> Adox etc.  
    'Set in advance a reference to COM adox ext 2.x for dll and security
    Public Sub Main()
        'cleanup old databases
        Dim catNewDB As New ADOX.Catalog
        Dim fi As New IO.FileInfo("c:\db1.mdb")
        If fi.Exists Then
            If MessageBox.Show("Delete?""Existing File db1.mdb", _
               MessageBoxButtons.YesNo) = DialogResult.Yes Then
                fi.Delete()
            Else
                Exit Sub
            End If
        End If
        catNewDB.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\db1.mdb")
        'To make tables we use Adonet
        Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "  Data Source=C:\db1.mdb;User Id=admin;Password=;")
        Dim cmd As New OleDb.OleDbCommand("CREATE TABLE persons ( " & _
               "AutoId int identity ," & _
               "Id int NOT NULL," & _
               "Name NVarchar(50)," & _
                 "BirthDate datetime," & _
                "IdCountry int," & _
                   "CONSTRAINT [pk_AutoId] PRIMARY KEY (AutoId)) ", conn)
        conn.Open()
        Try
            cmd.ExecuteNonQuery()
        Catch ex As OleDb.OleDbException
            MessageBox.Show(ex.Message, "OleDbException")
            Exit Sub
        Catch ex As Exception
            MessageBox.Show(ex.Message, "GeneralException")
            Exit Sub
        End Try
        conn.Close()
    End Sub
End Module



1 Previous Next