Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all. I am having a problem when trying to create a database using mysql command. The code i am using is...
using (MySqlConnection con = connect_db())
            {

                con.Open();

                MySqlCommand cmd = new MySqlCommand("CREATE DATABASE @name;", con);

                cmd.Parameters.AddWithValue("@name", "fancydb");

                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception exc)
                {                   

                    return;
                
                }

                cmd.Dispose();

                con.Close();

                con.Dispose();
            
            }

When i try to run this code i always get an error saying that i have an error in mysql syntax near "fancydb"...but when I put the name in the command like: "CREATE DATABASE facnydb;" it works. Can anyone explain to me why is the error only happening when i try and use parameters?...any help will be much appreciated, thanking you in advance.
Posted
Comments
Zoltán Zörgő 27-May-13 6:55am    
I am not sure, but I think the driver can't figure out parameter type. You could try
cmd.Parameters.Add("@name", MySqlDbType.VarChar, 80).Value = "fancydb";
Ruwaldo 27-May-13 6:57am    
I read that you can only use parameters when you are manipulating data. So i am just gonna have to go without it.
This error comes at compile time or at runtime ?
Ruwaldo 27-May-13 7:18am    
runtime
At which line ?

1 solution

You can't use parameters in CREATE DATABASE or other DDL commands.

Here i mention reference link.
Creating, Altering, and Removing Databases[^]
 
Share this answer
 
v2
Comments
Ruwaldo 27-May-13 7:23am    
Yes only on DML commands i think.
renish patel 27-May-13 7:26am    
if you get solution then mark as answered..Thanks

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