process 调用 sqlplus.exe 最后退出的问题

因为客户都用着我们同一套客户端的oracle DB,写了好鬼多PACKAGE什么的,一开始还没什么,后来越来越多就开始麻烦了,所以设计为不同客户端版本的客户UPDATE DB的工具就很重要了

下面是一段比较核心的执行代码

(来自stackoverflow里的一个answer  http://stackoverflow.com/questions/650098/how-to-execute-an-sql-script-file-using-c

Process p = new Process();
p.StartInfo.UseShellExecute
= false;
p.StartInfo.RedirectStandardOutput
= true;
p.StartInfo.FileName
= "sqlplus";
p.StartInfo.Arguments
= string.Format("xxx/xxx@{0} @{1}", in_database, s);

bool started = p.Start();
// important ... read stream input before waiting for exit.
// this avoids deadlock.
string output = p.StandardOutput.ReadToEnd();

p.WaitForExit();

Console.WriteLine(output);

if (p.ExitCode != 0)
{
Console.WriteLine(
string.Format("*** Failed : {0} - {1}",s,p.ExitCode));
break;
}

这里出现一个问题  因为开出来的sqlplus.exe还没关闭  所以command还需要来多个quit或exit才能退出  所以调用时除了个exitcode为3的错误

找到了几种可能的方法

在DOS下执行这个呢  可以有3种方式

1. 一种是将@换成< 如 sqlplus username/password@server/dbname < XXX.sql

这样的话执行完这个sql文件就会自动退出sqlplus  但是不晓得为何我用之在程序中又不行  这个我到做完得时候还是没找出原因来  猜想可能是这个符号是DOS下特有的东西  独立开个PROCESS出来运行sqlplus没那个效果

2. 在DOS下执行  echo set define off | sqlplus username/password@server/dbname < XXX.sql

但是这种方式没办法放到同个PROCESS里  或者调用的是DOS的EXE?  这个求解哈  这部分不是很熟

3. 最后这种也是我最后采用来折中实现的

http://stackoverflow.com/questions/2809267/problem-running-oracle-script-from-command-line-using-sqlplus 

这里有人提到可以在sql文件的末尾添加入"quit;"来实现退出  我估计上面找到的那个方法那个人为什么能执行通过就是因为这样的

所以我设置了一个相对目录的文件作为临时文件  无论是读取文件还是从文本框进来的SQL都将生成到该文件中并在末尾处插入  然后也就那么实现了

做WEB做太久了  这些比较基本的东西老生疏哈

posted @ 2011-06-01 16:41  lavandachen  阅读(944)  评论(0编辑  收藏  举报