Reputation: 9648
I create a process in C# to execute sqlcmd /S <servername> /d <dbname> /E /i
to run sql script that create tables, views, stored procedures.
The problem is the process does not terminate after the execution is completed. I want to know how can I terminate sqlcmd immediately after the operation is completed.
Is there any input argument to sqlcmd that I can specify to terminate the process right away or can I do it directly in C#?
Update
This is the C# code that I use to execute the process.
foreach (var file in files)
{
////var begin = DateTime.Now;
////context.TrackBuildWarning(string.Format("Start exec sql file at {0}.", DateTime.Now));
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "sqlcmd.exe";
process.StartInfo.Arguments = string.Format("-S {0} -d {1} -i \"{2}\" -U {3} -P {4}", sqlServerName, databaseName, file, sqlUserName, sqlPassword);
process.StartInfo.WorkingDirectory = @"C:\";
process.Start();
process.WaitForExit(); // if I comment out this code it will execute much faster
////context.TrackBuildWarning(string.Format("Finished exec sql file at {0} total time {1} milliseconds.", DateTime.Now, DateTime.Now.Subtract(begin).TotalMilliseconds));
}
As you can see, I have comment that if I remove (or comment) out process.WaitForExit()
it will execute A LOT faster.
Upvotes: 35
Views: 27274
Reputation: 2793
Just use -Q (uppercase).
This command selects 'asd' and quits immediately:
sqlcmd -S (local) -Q "select('asd')"
Upvotes: 60
Reputation: 1211
Do you have quit or exit at the end of your SQL file?
See here for the sqlcmd commands.
Upvotes: 1