public DatabaseCreateResult RunScript(
SqlConnectionString conString, string file, string variables)
{
using (var proc = new Process())
{
proc.StartInfo.FileName = SqlCmd;
if (conString.Authentication == false)
{
proc.StartInfo.Arguments = String.Format(@"-U{0} -P{1}", conString.UserID, conString.Password);
}
else
{
proc.StartInfo.Arguments += String.Format(" -b -E ");
}
proc.StartInfo.Arguments += variables;
proc.StartInfo.Arguments += String.Format(@"-S{0} -i""{1}""", conString.Server, file);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
LogMessage(
MessageImportance.Normal,
string.Format("Executing SQLCmd: {0}", proc.StartInfo.Arguments));
proc.Start();
var result = new StringBuilder(proc.StandardOutput.ReadToEnd());
proc.WaitForExit();
result.Append(proc.StandardOutput.ReadToEnd());
LogMessage(
MessageImportance.Normal,
string.Format("Execution Results: {0}", result.ToString()));
return new DatabaseCreateResult { Output = result.ToString(), IsSuccessful = proc.ExitCode != 1 };
}
}