I have a method in my C# code that executes an SQL script. If all the values in the script are hard coded, it works fine. But if I try to call a script that uses variables in the form $(varName), I have no idea how to substitute those in my code.
How would I go about executing a sql script while passing it all of its variables that are in the above form?
I am trying to replicate the -v flag for the SQLCMD Batch Command if anyone has experience with that.
Thank you,
Code:
public static bool executeSqlScript(string scriptPath, string serverName, string databaseName)
{
SqlConnection connection = null;
try
{
string connectionString = @"Server=" + serverName + ";Database=" + databaseName + ";Integrated Security=true";
string scriptText = System.IO.File.ReadAllText(scriptPath);
connection = new SqlConnection(connectionString); //was declared above
SqlCommand command = new SqlCommand(scriptText, connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
return true;
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
}