Create a Table at runtime. C#

Asked

Viewed 613 times

0

Hello,
I need to create a table within a DB Access at runtime.

The Database already exists, I would just like to click on a buttona new tablewas created within this DB with Nome de Campos and Data Type all pre-defined, so that all new tablesare created with the same fields.

        OleDbConnection ConSelect = new OleDbConnection();
        ConSelect.ConnectionString = Properties.Settings.Default.dbteste;

        ConSelect.Open();
        OleDbCommand command = new OleDbCommand();

        command.CommandText = "CREATE TABLE MyTable (" +
                        "[Count] INT NOT NULL AUTO_INCREMENT PRIMARY KEY ," +
                        "[TimeAndDate] TIMESTAMP NOT NULL ," +
                        "[SerialNumber] VARCHAR( 14 ) NOT NULL ," +
                        "[Result] BOOL NOT NULL ," +
                        "UNIQUE ([TimeAndDate]))";

        command.ExecuteNonQuery();

        ConSelect.Close();

This is a way I was trying, but still of error. -> Additional information: Executenonquery: Connection Property has not been initialized. <-

  • What have you done? What is your specific difficulty?

  • @Currently in my company, every sale I make, I make the INSERT of each product sold. (Within one DB, I have a TbVendas, where I do all the INSERTs). Now I need to create a new one Table for each sale made. (In an Evento_click, Create a tbVendas2508 Within a DB with the Fields already predefined. (Automatic_cod, Descprod, Precoprod... ) and then do the INSERTinside that new Table. About this problem I have nothing yet, Work with direct access to bank in Oledb, Access.

  • What will this help in your application? And post what you’ve already done.

  • @GOKUSSJ4 When making the products out of my stock, I need to add them to a output note. so I’d like to create this Tb to use it as his own output note. So it needs to be in RunTime, so that each time necessary to create a Output note I can create a new Table.

  • Why can’t you leave the Notes table created and do the Insert in it ? which forces you to create a table?

  • @Intruder I work with Auto Parts Export, I live in Miami and send to Brazil. Every week I make a Outgoing Note with about 1200 items, or about 800Rows. At the end of only 1 year would be more than 150milRows in a TABLE. I find a lot to do a research or a migration if one day it is necessary. That’s why I wanted to separate one TABLE for each Note. I can even think of creating a TABLE for each month, to become lighter, but still, would need to create in RunTime... But Thanks for your attention, I got the code and put as response. Hugs!

Show 1 more comment

1 answer

0

Good morning! I managed to create the table in Runtime, from this code.

        OleDbConnection Con = new OleDbConnection();
        Con.ConnectionString = Properties.Settings.Default.dbteste;

        Con.Open();
        OleDbCommand Cmm = new OleDbCommand();
        Cmm.CommandText = "CREATE TABLE NOVAFASE (" +
           "[Count] AUTOINCREMENT NOT NULL PRIMARY KEY," +
           "[TimeAndDate] VARCHAR(14) NOT NULL," +
           "[SerialNumber] VARCHAR(14) NOT NULL," +
           "[Result] VARCHAR(14) NOT NULL," +
           "UNIQUE([Count]))";


        Cmm.CommandType = CommandType.Text;
        Cmm.Connection = Con;

        Cmm.ExecuteNonQuery();
        MessageBox.Show("Inclusão efetuada com Sucesso !");

       Con.Close();

Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.