使用 C# 在 Db2 上绑定
Bind on Db2 using C#
我想使用 C# 在 z/OS V12 的 Db2 上执行绑定语句。在 db2cmd 中它工作正常:
db2 bind filename.bnd owner OOO qualifier QQQ collection CCC explain no grant public
在 C# 中,我启动了一个 db2cmd 进程。我想写入 StandardInput 并记录输出,但都不起作用:命令没有执行,我没有得到任何输出。
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();
}
我做错了什么?有更好的解决方案吗?
通过向 db2cmd.exe 程序提供参数 -i
解决了该问题。
这个参数是documented为:
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."
我想使用 C# 在 z/OS V12 的 Db2 上执行绑定语句。在 db2cmd 中它工作正常:
db2 bind filename.bnd owner OOO qualifier QQQ collection CCC explain no grant public
在 C# 中,我启动了一个 db2cmd 进程。我想写入 StandardInput 并记录输出,但都不起作用:命令没有执行,我没有得到任何输出。
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();
}
我做错了什么?有更好的解决方案吗?
通过向 db2cmd.exe 程序提供参数 -i
解决了该问题。
这个参数是documented为:
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."