I had a problem trying to create a database using the mysql command. The code I'm 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 the error message that I have
error in mysql syntax near "fancydb"
but when I enter the name in the command: "CREATE DATABASE facnydb;" , it works. Can someone explain to me why the error only occurs when trying to use parameters?
source share