I wrote a method to execute SQL commands with parameters. But I get an exception stating there is a syntax error near @p0
.
private const string CATALOG_NAME = "test";
ExecuteSqlCommand(@"CREATE DATABASE @p0", CATALOG_NAME);
private void ExecuteSqlCommand(string query, params object[] parameters)
{
try
{
using (var command = new SqlCommand(query, _connection))
{
for (int i = 0; i < parameters.Length; i++)
{
command.Parameters.Add(new SqlParameter("@p" + i, parameters[i]));
}
command.ExecuteNonQuery();
}
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
I also tried AddWithValue
and get the same error. I looked into the command with debugging and everything seems fine.