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.
To exit sqlcmd, type EXIT or QUIT at the start of a new line. To clear the statement cache, type :RESET. Typing ^C causes sqlcmd to exit. ^C can also be used to stop the execution of the statement cache after a GO command has been issued.
The noexec method Another method that works with GO statements is set noexec on (docs). This causes the rest of the script to be skipped over. It does not terminate the connection, but you need to turn noexec off again before any commands will execute.
You can test SQLCMD.exe from a Powershell console, running as the Delphix OS user, on the Windows host where Delphix Connector service is running.
The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files through a variety of available modes: At the command prompt. In Query Editor in SQLCMD mode.
Just use -Q (uppercase).
This command selects 'asd' and quits immediately: sqlcmd -S (local) -Q "select('asd')"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With