How do i create two tables in a database
at same time from a windows form.??

how do i give them same name??

Recommended Answers

All 4 Replies

You'd execute a non-query on the database using a create table command. The syntax of the command would depend on the database you are using.

You can't have two tables with the same name, how would the system know what table you want to use?

Do you mean a mirror table, like some sort of backup?

@ddanbe...

naah.. i want tables with same name to be stored..
i know its not possible in same DB..but i tried doing it with two different DBs
n it din work either..

lemme paste my code....

public void button1_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == "")
            {
                textBox1.Focus();
                errorProvider1.SetError(textBox1, "Please Enter Name of the company");
                MessageBox.Show("Enter the name of company");


            }
            else
            {
                
                string str;
                str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Program Files\\DB1.accdb";

               string str2 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Program Files\\DB2.accdb";


                OleDbConnection cd = new OleDbConnection(str);
                cd.Open();


                string strrr = "select * from Table1";
                OleDbCommand cmdb = new OleDbCommand(strrr, cd);

                OleDbDataReader drr = cmdb.ExecuteReader();

                if (drr.Read())
                {
                    MessageBox.Show("noar");
                }

                try
                {

                    string qry = "insert into Table1 values('" + textBox1.Text + "')";
                    OleDbCommand cmd = new OleDbCommand(qry, cd);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Company added successfully");
                    OleDbCommand ccm = cd.CreateCommand();

                   //table1

                    ccm.CommandText = "CREATE TABLE " + textBox1.Text + "(Nmb1 int, Nmb2 int)";
    
                    ccm.ExecuteNonQuery();

                    MessageBox.Show("Added");
                    cd.Close();

                    ////table2
                   OleDbConnection con1 = new OleDbConnection(str2);

                    con1.Open();

                   OleDbCommand cmm = con1.CreateCommand(); 

                   cmm.CommandText = "CREATE TABLE " + textBox2.Text + "(Password CHAR(10))";

                    cmm.ExecuteNonQuery();
                    con1.Close();


                    MessageBox.Show("asdasd");

                    Form2 f2 = new Form2();
                    f2.Show();
                                      
                   

                }
                 

                catch(Exception)
                {
                    MessageBox.Show("Already exist");
                }
                cd.Close();
            }

Your code is kind of messy.
Once you do it for one table on one DB there's no dificulty in doing it on the other, same process, different connection.
Setup different connections, different commands and execute.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.