Search code examples
c#cmddb2

Bind on Db2 using C#


I want to execute bind statements on Db2 for z/OS V12 using C#. In db2cmd it works fine:

db2 bind filename.bnd owner OOO qualifier QQQ collection CCC explain no grant public

In C# I start a db2cmd process. I want to write to the StandardInput and log the output, but neither worked: The commands are not executed and I do not get any output back.

using (var p = new Process
{
    StartInfo =
    {
        FileName = "db2cmd",
        UseShellExecute = false,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true
    }
})
{    
    p.OutputDataReceived += (sender, e) => { Console.WriteLine(e.Data); };
    p.ErrorDataReceived += (sender, e) => { Console.WriteLine(e.Data); };

    p.Start();
    p.BeginOutputReadLine();
    p.BeginErrorReadLine();

    p.StandardInput.WriteLine($"db2 connect to {DataBase} user {User.Userid} using {User.Password}");
    p.StandardInput.WriteLine("db2 bind filename.bnd owner OOO qualifier QQQ collection CCC explain no grant public");

    p.WaitForExit();
}

What am I doing wrong? Is there a better solution?


Solution

  • The problem was resolved by supplying the argument -i to the db2cmd.exe program.

    This argument is documented as:

    Run command following the -i option while sharing Db2 command window and inheriting file handles. For example, db2cmd -i dir runs the dir command in the same Db2 command window."