I have to modify a SQL Server database dynamically, depending on some inputs. What I wanted to do is to generate the modification commands and then execute them. And, to avoid SQL injection, I wanted to use parameters. But it doesn't work.
Here is a snippet of my PoC code (just a rough thing):
public static void ModifyDatabase(string name)
{
string connectionString = "Server=localhost; Database=DEV_Tests; Integrated Security=True;";
var sqlCommand = "CREATE SCHEMA @schemaname";
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(sqlCommand, connection))
{
connection.Open();
command.Parameters.AddWithValue("@schemaname", name);
command.ExecuteNonQuery();
connection.Close();
}
}
This throws a syntax error. But this one works:
public static void ModifyDatabase()
{
string connectionString = "Server=localhost; Database=DEV_Tests; Integrated Security=True;";
var sqlCommand = "CREATE SCHEMA AAAA";
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(sqlCommand, connection))
{
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
So, or I'm doing something wrong (could be), or I can't use parameters with the "create schema". In fact, in the second one (the one without params), if I add a parameter, without using it, fails too. Is that so? If we can't use params here, what would be the best way to sanitize the input? Trim it and remove conflictive parameters?
Thanks.