programatically add table in access

D

Dave

I have an application already deployed in c# that uses an access database. I
need to add a table to the database programatically, so the updated program
can simply check for the table and add it if necessay. How would I do that?
 
J

Joe Cool

I have an application already deployed in c# that uses an access database. I
need to add a table to the database programatically, so the updated program
can simply check for the table and add it if necessay. How would I do that?

Have you cheked into the Create Table SQL command?
 
A

Arne Vajhøj

Dave said:
I have an application already deployed in c# that uses an access database. I
need to add a table to the database programatically, so the updated program
can simply check for the table and add it if necessay. How would I do that?

Try and select from it if you get an exception then create it.

Arne
 
D

Dave

I'm using the following code to add tables to the database,

chkTables = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=|DataDirectory|\\test.mdb" );
chkTables.Open();

string commandString = "CREATE TABLE Payments_Commis (ID Guid
Primary Key, Pay_Date DateTime, Company_no Text (255), Ab Long, Description
Text(255), Amount DOUBLE )";
OleDbCommand commMake = new OleDbCommand(commandString,
chkTables);
commMake.ExecuteNonQuery();

This works fine, but I would like to know how to add a AutoNumber field, and
how to set the double field Amount to 2 decimal places. The folowing does not
work

string commandString = "CREATE TABLE Payments_Commis (ID Guid AutoNumber,
Pay_Date DateTime, Company_no Text (255), Ab Long, Description Text(255),
Amount DOUBLE (2))";

both give errors.
 
P

Paul Shapiro

You specifed a guid data type with the autonumber property. As far as I
knew, Access autonumbers are only long integers, not guids. In SQL Server
you can default a guid datatype with a newGUID() builtin function, but check
the documentation for Access. Double(2) is definitely not going to be Access
syntax, and it wouldn't be sql server either.

Check the Access SQL documentation. Autonumber is a field attribute, as is
the format property which you would use to set the displayed decimal places.
If you want the number stored as a decimal with 2 decimal places, use the
decimal field type instead of double. The format property has to be created
and attached to the field before you can set a value. I think Access sql
would be unlikely to support this. But it's only a display property, and
would not affect the stored data values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top