Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having problem regarding this code. I try to create table based on database that user defined in c#.net framework.


SchemaName schemaForm = new SchemaName();
            frmMetCon form2 = new frmMetCon();

            if (schemaForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
               
              string connStr = "datasource=localhost;port=3306;username=root;password=root;";
              MySqlConnection conn = new MySqlConnection(connStr);
                try
                 {
                   MySqlCommand command = conn.CreateCommand();
                   conn.Open();
                   command.CommandText = "DROP DATABASE IF EXISTS " + schemaForm.getData();
                   command.ExecuteNonQuery();
                   command.CommandText = "CREATE DATABASE " + schemaForm.getData();
                   command.CommandText = "CREATE TABLE Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
                   command.ExecuteNonQuery();
                   this.Hide();
                   form2.Show();   
                 }
                 catch (Exception ex)
                 {
                   MessageBox.Show(ex.Message); 
                 }
                 conn.Close();
           
             }


The error showing that no database selected. if I create like this

command.CommandText = "CREATE TABLE" +schemaForm.getData()+ ".Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";


I will get error on the command. Please help
Posted

I think you have a problem with your connection string
check the following MySQL connection strings
 
Share this answer
 
Comments
arave0521 12-Oct-13 13:28pm    
There is nothing wrong with the connection. It works fine. The proble I'm facing is the command of creating table based on user defined the database name
command.CommandText = "CREATE TABLE" +schemaForm.getData()+ ".Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
Before you execute the Create table command you must execute the create database command, Like

try
                {
                  MySqlCommand command = conn.CreateCommand();
                  conn.Open();
                  command.CommandText = "DROP DATABASE IF EXISTS " + schemaForm.getData();
                  command.ExecuteNonQuery();
                  command.CommandText = "CREATE DATABASE " + schemaForm.getData();
                    command.ExecuteNonQuery();
                  command.CommandText = "CREATE TABLE Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
                  command.ExecuteNonQuery();
                  this.Hide();
                  form2.Show();
                }
 
Share this answer
 
Comments
arave0521 12-Oct-13 13:40pm    
I tried but still got error. The error said that no database selected. if I create

try
{
MySqlCommand command = conn.CreateCommand();
conn.Open();
command.CommandText = "DROP DATABASE IF EXISTS " + schemaForm.getData();
command.ExecuteNonQuery();
command.CommandText = "CREATE DATABASE " + schemaForm.getData();
command.ExecuteNonQuery();
command.Parameters.AddWithValue("@schema",database);
command.CommandText = "CREATE TABLE @schema.Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
command.ExecuteNonQuery();
this.Hide();
form2.Show();
}

there is still an error
[no name] 12-Oct-13 13:50pm    
Oh yes now you show open connection with the new Database you create now and execute the create table query with the new command.
arave0521 12-Oct-13 13:51pm    
I don't get it. do you mind showing example?
[no name] 12-Oct-13 14:08pm    
try
{
MySqlCommand command = conn.CreateCommand();
conn.Open();
command.CommandText = "DROP DATABASE IF EXISTS " + schemaForm.getData();
command.ExecuteNonQuery();
command.CommandText = "CREATE DATABASE " + schemaForm.getData();
command.ExecuteNonQuery();
conn.close();
command.ExecuteNonQuery();
con.close();
con.connsectionstring=Connectionstring;// this will contain the same server information but database information is the new one,
comand=new conn.CreateCommand()
command.CommandText = "CREATE TABLE Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
command.ExecuteNonQuery();
this.Hide();
form2.Show();
}
arave0521 12-Oct-13 14:27pm    
there is an error on con.connsectionstring=Connectionstring;// this will contain the same server information but database information is the new one,
SchemaName schemaForm = new SchemaName();
            frmMetCon form2 = new frmMetCon();
 
            if (schemaForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
               
              string connStr = "datasource=localhost;port=3306;username=root;password=root;";
              MySqlConnection conn = new MySqlConnection(connStr);
                try
                 {
                   MySqlCommand command = conn.CreateCommand();
                   conn.Open();
                   command.CommandText = "DROP DATABASE IF EXISTS " + schemaForm.getData();
                   command.ExecuteNonQuery();
                   command.CommandText = "CREATE DATABASE " + schemaForm.getData();
                   command.ExecuteNonQuery();
                   command.CommandText = "USE " + schemaForm.getData();
                   command.ExecuteNonQuery();
                   command.CommandText = "CREATE TABLE Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
                   command.ExecuteNonQuery();
                   this.Hide();
                   form2.Show();   
                 }
                 catch (Exception ex)
                 {
                   MessageBox.Show(ex.Message); 
                 }
                 conn.Close();
           
             }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900