YAKOVM
YAKOVM

Reputation: 10153

Catch osql exception

I try to make login using osql. I know that while running from cmd the exeption is thrown But in the following code I don`t catch an exception. How can I catch an exception?

p.StartInfo.FileName = "osql.exe";
p.StartInfo.Arguments = "-S serverName -U userName -P \"psw\" -d db";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
try
{
   p.Start();
   // should not get here
}
catch(Exception ex)
{
   // should get here
}

Upvotes: 0

Views: 141

Answers (1)

Cadburry
Cadburry

Reputation: 1864

The System.Diagnostic.Process 'p' won't raise an exception, if the login fails on osql.exe. Maybe the result of osql.exe will return an win32-err code... You cannot handle a loginexception from another process in your application by using a try catch block

EDIT:

You can receive the ExitCode by P.ExitCode. Consider to wait for the osql.exe process to exit by using the WaitForExit() Method.

   p.Start();            
   p.WaitForExit(); //Wait for the Application to exit
   if (p.ExitCode <> 0) //If <>0 the operation failed
     { ///Operation failed
     }

On my machine the result was always 0 or 1 - i dont think you will get an exact error code for a loginerror. Just a failed or success information.

Upvotes: 1

Related Questions