Process class db2cmd c#

1.1k views Asked by At

I have my program which needs to run a ".BAT" file in IBM DB2 (db2cmd.exe). And log the contents of that console into a string, which I should be able to format.

Status quo is: The bat file contains username and password to the database, Export to csv query. The bat file when executed manually works absolutely fine.

The problem is that I am not able to capture the details of that console into a string.

Code snippet is as follows:

proc.StartInfo.FileName = "db2cmd.exe";
proc.StartInfo.Arguments = @"C:\test.bat";
proc.StartInfo.WorkingDirectory = @"C:\Program Files\IBM\SQLLIB\BIN\";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;

proc.OutputDataReceived += (o, e) => s.AppendLine(e.Data);
proc.ErrorDataReceived += (o, e) => s.AppendLine(e.Data);

proc.Start();

proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();

code = proc.ExitCode;
2

There are 2 answers

0
Bimal Jha On

Using -i option with db2cmd.exe should fix this issue. Modify first line of your program as below:

proc.StartInfo.FileName = "db2cmd.exe -i";
0
mustaccio On

db2cmd.exe opens a new command shell by default. Try using the command switches /i /c to run your script in the same shell.