adding table at runtime?

  • Thread starter Thread starter David Smith
  • Start date Start date
D

David Smith

I want to add a table in my ms access database at runtime. through Vb.NET.
any ideas?
 
Easy Peasy,

I just tested the following code for you and it works just fine and dandy.
You just need to alter the table names and DataAdapter and Connection object
names to suite your own code requirements.

Regards - OHM

Dim sqlCmd As String = "SELECT People.FirstName INTO Something FROM People;"

Dim cmd As New OleDb.OleDbCommand(sqlCmd, Me.con)

cmd.CommandType = CommandType.Text

con.Open()

' This executes the Non Query and returns the number of rows affected
MessageBox.Show(cmd.ExecuteNonQuery.ToString())

con.Close()

End Sub
 
If you are talking about creating a completely new table design, then thats
a different matter all together !?!

Regards - OHM
 
David Smith said:
I want to add a table in my ms access database at runtime. through
Vb.NET. any ideas?

Do you have the Access SQL reference available? You'll find the right
statement there ("CREATE TABLE..."). Use ADO.NET to execute the SQL. There
are ADO.NET chapters in the .NET Framework docs and in the VB/C# docs.
There's also a newsgroup for ADO.NET:
microsoft.public.dotnet.framework.adonet.
 
Hi David,

You can try this
\\
Dim conn1 As OleDb.OleDbConnection = New OleDb.OleDbConnection
conn1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\test1\db.mdb;User Id=admin;Password=;"
conn1.Open()
Dim cmdA As New OleDb.OleDbCommand( _
"CREATE TABLE Sites (n1 char(50) NOT NULL," & _
"n2 Char(20)," & _
"n3 Char(20)," & _
"n4 Char(20)," & _
"n5 Char(20)," & _
"CONSTRAINT [pk_n1] PRIMARY KEY (n1))", conn1)
cmdA.ExecuteNonQuery()
///

I hope this helps you?

Cor
I want to add a table in my ms access database at runtime. through Vb.NET.
any ideas?
 
Or . . .

You can Add a Reference to your project to include the Microsoft Access
Database, that way you can access it programatically rather than using SQL
statements.

Regards - OHM
 
Hi Herfried,

Tip of the day 2 ,

Try this

Cor
You can try this
\\
Dim conn1 As OleDb.OleDbConnection = New OleDb.OleDbConnection
conn1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\test1\db.mdb;User Id=admin;Password=;"
conn1.Open()
Dim cmdA As New OleDb.OleDbCommand( _
"CREATE TABLE Herfried (n1 char(50) NOT NULL," & _
"n2 Char(20)," & _
"n3 Char(20)," & _
"n4 Char(20)," & _
"n5 Char(20)," & _
"CONSTRAINT [pk_n1] PRIMARY KEY (n1))", conn1)
cmdA.ExecuteNonQuery()
///
 
* "Cor said:
Tip of the day 2 ,

Try this

ACK. If you start an application with a "tip of the day" box more than
once a day, you will see different tips.

;-)
 
Back
Top