Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm having a problem regarding renaming a schema of MySQL in Visual C#. The idea is that when user debug the program, he needs to choose either create new project or open project. When user click a button of new project, the namingForm will pop up. User will enter the name (which has a rules later on naming it) and the name will be used as schema name. here is the code

C#
private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            SchemaName schemaForm = new SchemaName();

            if (schemaForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
              string connStr = "datasource=localhost;port=3306;username=root;password=root;";
              MySqlConnection conn = new MySqlConnection(connStr);
                try
                 {
                   conn.Open();
                   MySqlCommand cmdDatabase = new MySqlCommand("CREATE SCHEMA @schema; ", conn);
                   cmdDatabase.Parameters.AddWithValue("@schema", schemaForm.getData());
                   MySqlDataReader myReader;
                   myReader = cmdDatabase.ExecuteReader();
                   myReader.Close();
                 }
                 catch (Exception ex)
                 {
                   MessageBox.Show(ex.Message); 
                 }
                 conn.Close();
           
             }
        }

Once executed, the error was pop up saying that you have error in your sql syntax
Posted
Updated 10-Oct-13 17:37pm
v2
Comments
Ron Beyer 10-Oct-13 23:43pm    
what is the value of schemaForm.getData()? Have you looked at the entire SQL command just before it tries to execute? Why are you using ExecuteReader when you should be using ExecuteNonQuery()?
arave0521 10-Oct-13 23:50pm    
The value of schemaForm.getData()is user define value which is I'm not stated. the code.
Here is the code of schemaForm

public partial class SchemaName : Form
{
private string data;

public SchemaName()
{
InitializeComponent();
}

private void btnCancel_Click(object sender, EventArgs e)
{
this.Hide();
}

private void btnOK_Click(object sender, EventArgs e)
{
data=txtDB.Text;
this.Hide();
}

public string getData()
{
return data;
}

}

The error given is that You have an error in your SQL syntax, check the manual that corresponds to your MYSQL server version for the right syntax to use near "user-defined-schemaName" at line 1
Thomas ktg 11-Oct-13 1:35am    
May be you can try using Database instead of Schema for create syntax like
create database @schema;
I suspect the Schema keyword is not supported for the MySql version.
arave0521 11-Oct-13 9:21am    
Schema is the right word. The only problem is I'm using a ExecuteReader(). I just solved it by using ExecuteNonQuery()
Ron Beyer 11-Oct-13 9:50am    
CREATE SCHEMA and CREATE DATABASE are synonymous since I think version 5.02. CREATE SCHEMA is the depreciated one, CREATE DATABASE is the one you should use for future compatibility.

1 solution

Just so this doesn't remain unanswered...

The solution was to replace ExecuteReader with ExecuteNonQuery to run a query that does not return a result.
 
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