Table and column names cannot be parameterized, but for the first line of protection, wrap the table name with a delimiter, such as curly braces,
string myQuery = "CREATE DATABASE [" + tbxDatabase.Text + "]";
or create a user definition function that checks the input value, for example
private bool IsValid(string tableName)
{
}
then in your code
if (IsValid(tbxDatabase.Text))
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
string myQuery = "CREATE DATABASE [" + tbxDatabase.Text + "]";
myConnection.Open();
SqlCommand myCommand = new SqlCommand(myQuery, myConnection);
myCommand.ExecuteNonQuery();
}
else
{
}
source
share