0

I am using the following code to execute osql command and then get its output (like 2 rows affected etc) but it never finishes. Please let me know what am I missing.

string sqlFilePath = Helper.GetFilePath(sqlFileName, Environment.CurrentDirectory);

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"osql -E -S @Server -d @Database -T -n -i ""@SqlFile"""
           .Replace("@Server", ConfigurationManager.AppSettings["Server"])
           .Replace("@Database", Path.GetFileNameWithoutExtension(DB))
           .Replace("@SqlFile", sqlFilePath);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();

string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
davmos
  • 9,324
  • 4
  • 40
  • 43
Akshay J
  • 5,362
  • 13
  • 68
  • 105

1 Answers1

1

I believe you might be running into two separate problems:

  • Set the FileName and Arguments properties like this:

    p.StartInfo.FileName = "osql.exe";
    p.StartInfo.Arguments = @"-E -S @Server -d @Database -T -n -i ""@SqlFile"""
        .Replace("@Server", ConfigurationManager.AppSettings["Server"])
        .Replace("@Database", Path.GetFileNameWithoutExtension(DB))
        .Replace("@SqlFile", sqlFilePath);
    
  • You might also encounter an encoding issue. make sure that you save you .sql file by using the Unicode Encoding (codepage 1200) (here's a question which describes the issue).

Community
  • 1
  • 1
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78