G
Guest
I have given my users the ability to create a new MS Access database. I want
to do this through code (VB.Net 2005). I am able to create the DB, add 1
table, and add constraints to it using SQL syntax in one pass. However this
DB has 10 tables in it and i want to add all 10 through one Try/Catch/Finally
pass. I've tried various combinations of syntax but have come up short. All
the documentation seems to tell me how to add one at a time.
BTW - I am adding contraints in a second pass so any help with adding
multiple constraints would be greatly appreciated.
Here is what i have so far for adding the first table & it works, adding the
second table in the same pass it the problem.
sCreateString = sCreateString & ";Persist Security Info=False"
Dim NewTabConn As New System.Data.OleDb.OleDbConnection
Dim NewTabCmd As OleDbCommand = New OleDbCommand
Dim AlterTabCmd As OleDbCommand = New OleDbCommand
NewTabConn = New OleDbConnection(sCreateString)
NewTabCmd.Connection = NewTabConn <--New table Command
AlterTabCmd.Connection = NewTabConn <--Alter table
Command
NewTabCmd.CommandText = "CREATE TABLE Committees " & _
"(Member_ID Long, " & _
"Committee TEXT(25)) " & _
"union all " & _ <--Desperate attemt to
try anything
"CREATE TABLE Family " & _
"(FamilyNO IDENTITY, " & _
"FamilyName TEXT(50))"
AlterTabCmd.CommandText = "Alter Table Committees " & _
"Add CONSTRAINT [PK_Committees] PRIMARY KEY (Member_ID, Committee) "
NewTabConn.Close()
Try
NewTabConn.Open()
NewTabCmd.ExecuteNonQuery()
AlterTabCmd.ExecuteNonQuery()
NewTabConn.Close()
Catch ex As Exception
MessageBox.Show("could not create table" & ex.ToString, "Create
table", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Thanks
Gary
to do this through code (VB.Net 2005). I am able to create the DB, add 1
table, and add constraints to it using SQL syntax in one pass. However this
DB has 10 tables in it and i want to add all 10 through one Try/Catch/Finally
pass. I've tried various combinations of syntax but have come up short. All
the documentation seems to tell me how to add one at a time.
BTW - I am adding contraints in a second pass so any help with adding
multiple constraints would be greatly appreciated.
Here is what i have so far for adding the first table & it works, adding the
second table in the same pass it the problem.
sCreateString = sCreateString & ";Persist Security Info=False"
Dim NewTabConn As New System.Data.OleDb.OleDbConnection
Dim NewTabCmd As OleDbCommand = New OleDbCommand
Dim AlterTabCmd As OleDbCommand = New OleDbCommand
NewTabConn = New OleDbConnection(sCreateString)
NewTabCmd.Connection = NewTabConn <--New table Command
AlterTabCmd.Connection = NewTabConn <--Alter table
Command
NewTabCmd.CommandText = "CREATE TABLE Committees " & _
"(Member_ID Long, " & _
"Committee TEXT(25)) " & _
"union all " & _ <--Desperate attemt to
try anything
"CREATE TABLE Family " & _
"(FamilyNO IDENTITY, " & _
"FamilyName TEXT(50))"
AlterTabCmd.CommandText = "Alter Table Committees " & _
"Add CONSTRAINT [PK_Committees] PRIMARY KEY (Member_ID, Committee) "
NewTabConn.Close()
Try
NewTabConn.Open()
NewTabCmd.ExecuteNonQuery()
AlterTabCmd.ExecuteNonQuery()
NewTabConn.Close()
Catch ex As Exception
MessageBox.Show("could not create table" & ex.ToString, "Create
table", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Thanks
Gary