Create MS Access database with ADO.NET

You cant. You need the program to produce an Access database.
ADO.net is designed for accessing a variety of database servers(and specifically optimized for SQL server), not creating databases.


Jon
 
Could I write code similar to:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class Class1

Private conn As SqlConnection
Private cmd As SqlCommand
Private sql As String

Public Function ExecuteSQL(ByVal sql As String)
cmd = New SqlCommand(sql, conn)
Try
cmd.ExecuteNonQuery()
Catch Ex As SqlException
MessageBox.Show(Ex.Message.ToString())
End Try
End Function

This method creates a new SQL Server database
Public Function CreateDatabase()
Create a connection
conn = New SqlConnection("Integrated Security=SSPI;" & "Initial Catalog=;" & "Data Source=localhost;")
Open the connection
If Not (conn.State = ConnectionState.Open) Then
conn.Open()
sql = "CREATE DATABASE mydb ON PRIMARY" & "(Name=mydb, filename = E:\Program Files\Microsoft SQL Server\MSSQL\data\mydb.mdf, size=3,"
sql = sql & "maxsize=5, filegrowth=10%)log on"
sql = sql & "(name=mydb_log, filename=E:\Program Files\Microsoft SQL Server\MSSQL\data\mydb_log.ldf,size=3,"
sql = sql & "maxsize=20,filegrowth=1)"
ExecuteSQL(sql)
conn.Close()
End If
End Function

This method creates the tables of the database
Public Function CreateTables()
Open the connection
If Not (conn.State = ConnectionState.Open) Then
conn = New SqlConnection("Integrated Security=SSPI;" & "Initial Catalog=mydb;" & "Data Source=localhost;")
Try
conn.Open()
sql = "CREATE TABLE EmailContacts" & "(ContactID INTEGER NOT NULL CONSTRAINT pk_EmailContacts_ContactID PRIMARY KEY,"
sql = sql & "FirstName NVARCHAR(20), LastName NVARCHAR(35), Email1 NVARCHAR(255))"
ExecuteSQL(sql)
Catch Ex As Exception
MessageBox.Show(Ex.Message.ToString())
End Try
End If
Try
Adding record to the table Emailcontacts
sql = "INSERT INTO EmailContacts(ContactID, FirstName, LastName, Email1)"
sql = sql & "VALUES(1,Rick, Dobson, rickd@cabinc.net)"
ExecuteSQL(sql)
Add second recoord to the table EmailContacts
sql = "INSERT INTO EmailContacts(ContactID, FirstName, LastName, Email1)"
sql = sql & "VALUES(2,Virginia, Dobson, virginia@cabinc.net)"
ExecuteSQL(sql)
conn.Close()
Catch Ex As Exception
MessageBox.Show(Ex.Message.ToString())
End Try
End Function
End Class

for create an Access database with the help of OleDb...

Sorry for my stupid questions...
 
For example what if I were connected to Access but not to Northwind....

Dim objConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "User ID=Admin;" & _
"Data Source=C:\Program Files\Microsoft" & _
"Office\Office10\Samples\Northwind.mdb" i dont want a specific database)

objConn.Open()

Execute the command
Dim objCmd As New OleDbCommand("CREATE DATABASE mydb.mdb .....bla bla...", objConn)
 
I found the solution. I added the reference Microsoft ADO Ext. for DLL and Security... and with Imports ADOX I did what I wanted
 
Back
Top