Create a Table at runtime. W#

0

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

The database already exists, I would just like that by clicking on a button a new table was created within this DB with Field Name and Type of Data all pre-defined so that all new tables are 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 the error.  - > Additional information: ExecuteNonQuery: Connection property has not been initialized.     

asked by anonymous 25.08.2016 / 22:57

1 answer

0

Good morning! I was able 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.

    
26.08.2016 / 15:35