是否有到 Oracle SQLPLUS 的 .Net 接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/657623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a .Net interface to Oracle SQLPLUS?
提问by Rich
I'm developing some automation to control the execution of SQL scripts. The scripts are run through SQL*PLUSand contain PL/SQLcalls in them (hence I can't run them through ODP.NET).
我正在开发一些自动化来控制 SQL 脚本的执行。这些脚本通过SQL*PLUS运行并在其中包含PL/SQL调用(因此我无法通过ODP.NET运行它们)。
I was wondering if there was a .NETinterface to SQL*PLUS? If so has anyone used it?
我想知道SQL*PLUS是否有.NET接口?如果有,有人用过吗?
回答by Pablo Santa Cruz
You can do it in C# with this piece of code:
您可以使用以下代码在 C# 中执行此操作:
public int execString(string scriptFileName)
{
int exitCode;
ProcessStartInfo processInfo;
Process process;
int timeout = 5000;
processInfo = new ProcessStartInfo("sqlplus.exe", "@" + scriptFileName);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
process = process.Start(ProcessInfo);
process.WaitForExit(timeout);
exitCode = process.ExitCode;
process.Close();
return exitCode;
}
In VB.NET you could accomplish the exact same thing, using the same API in the framework, but I don't know much about VB.NET syntax.
在 VB.NET 中,您可以完成完全相同的事情,在框架中使用相同的 API,但我对 VB.NET 语法知之甚少。
You could also try inspecting SQL/Plus DLLs and see if you can get something out of them. But I think that even though it should be a faster (performance wise) approach, it will be way more complicated than using what I am suggesting.
您还可以尝试检查 SQL/Plus DLL,看看是否可以从中得到一些东西。但我认为,即使它应该是一种更快(性能明智)的方法,它也会比使用我建议的方法复杂得多。
回答by Pablo Santa Cruz
It took me a while to figure out how to make it all work so here is the result of my investigations:
我花了一段时间才弄清楚如何让它全部工作,所以这是我的调查结果:
c# code:
代码:
ORAUtils.execString(@"c:\tmp.sql 'Oracle sucks!'");
...
using System.Diagnostics; -- where the Process stuff lives
...
public static int execString(string scriptFileName)
{
...
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = "sqlplus.exe";
processInfo.Arguments = "user/pwd@db @" + scriptFileName;
...
Process process = Process.Start(processInfo); // typo in code above small p instead of caps helps
...
Resulting command line:
结果命令行:
sqlplus.exe user/pwd@db @c:\tmp.sql 'Oracle sucks!'
sqlplus.exe 用户/pwd@db @c:\tmp.sql 'Oracle 糟透了!'
Type sqlplus /? in a dos prompt and you'll get the syntax:
键入 sqlplus /? 在 dos 提示中,您将获得语法:
sqlplus
sqlplus
Here logon=user/pwd@db and start=@c:\tmp.sql 'Oracle sucks!'
这里 logon=user/pwd@db 和 start=@c:\tmp.sql 'Oracle 糟透了!'
It will start the sql file and pass it the parameter string.
它将启动 sql 文件并将参数字符串传递给它。
tmp.sql first line:
tmp.sql 第一行:
prompt &1
提示 &1
will display the parameter string.
将显示参数字符串。
Thx
谢谢