Update:

having just tested, you can use "SET NOEXEC ON" directly with osql.exe.

I have even built it into an nunit test:

       [Test]
       public void ValidateSqlSyntax() {
const string connectionString = @"-U myuser -P mypassword -d mydatabase -S NBDev-01\SQLEXPRESS";
           using (var reader = new StreamReader("thefile.sql")) {
               CheckSyntax(connectionString, reader.ReadToEnd());
           }
       }

private static void CheckSyntax(string osqlConnectionString, string sql) {
           string command = string.Format(@"
SET NOEXEC ON

GO

{0}

GO

SET NOEXEC OFF
", sql);
           using (StreamWriter sw = new StreamWriter("temp.sql")) {
               sw.Write(command);
           }
string output = ProcessUtil.RunProcess("osql.exe", string.Format("-i temp.sql {0} -n", osqlConnectionString), null, null, 0);
           Assert.AreEqual("", output, "Error in sql command");
       }

   public static class ProcessUtil {
public static string RunProcess(string file, string args, string input, string workingdir, int waitTime) {
           var result = new StringBuilder();
           var p = new Process();
           var info = args == null
               ? new ProcessStartInfo(file)
               : new ProcessStartInfo(file, args);

           if (workingdir == null) {
               info.WorkingDirectory = workingdir;
           }
           info.UseShellExecute = false;
           info.RedirectStandardOutput = true;
           info.RedirectStandardError = true;
           if (input != null) {
               info.RedirectStandardInput = true;
           }
           p.StartInfo = info;
           if (p.Start()) {
               using (p) {
                   if (input != null) {
                       p.StandardInput.Write(input);
                       p.StandardInput.Close();
                   }
                   var error = new Thread(delegate() {
                       if (p.StandardError != null) {
                           string s;
                           lock (p.StandardError) {
                               s = p.StandardError.ReadToEnd();
                           }
                           lock (result) {
                               result.Append(s);
                           }
                       }
                   });
                   var output = new Thread(delegate() {
                       if (p.StandardOutput != null) {
                           string s;
                           lock (p.StandardOutput) {
                               s = p.StandardOutput.ReadToEnd();
                           }
                           lock (result) {
                               result.Append(s);
                           }
                       }
                   });
                   error.Start();
                   output.Start();
                   int wait = 0;
                   while (error.IsAlive || output.IsAlive) {
                       Thread.Sleep(50);
                       wait += 50;
                       if (wait > (waitTime > 0 ? waitTime : 3600000)) {
                           if (error.IsAlive) {
                               error.Abort();
                           }
                           if (output.IsAlive) {
                               output.Abort();
                           }
                           break;
                       }
                   }
                   if (!p.HasExited) {
                       p.Kill();
                       result.Append("\nError: process terminated\n");
                   }
               }
           }
           return result.ToString();
       }
   }

Reply via email to