在 C# 中的单个 Oracle 命令中执行多个查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31917301/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:58:45  来源:igfitidea点击:

Execute multiple queries in single Oracle command in C#

c#oraclevisual-studioplsql

提问by Hanni

I am using visual studio 2013 and oracle database.I want execute multiple create table queries at once in single oraclecommand is it possible ? I am trying following but not working

我正在使用 Visual Studio 2013 和 oracle 数据库。我想在单个 oraclecommand 中一次执行多个创建表查询是否可能?我正在尝试关注但不工作

OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandText = "create table test(name varchar2(50) not null)"+"create table test2(name varchar2(50) not null)"; 
//+ "create table test3(name varchar2(50) not null)"+"create table test3(name varchar2(50) not null)";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();

Got error at cmd.ExecuteNonQuery();

在 cmd.ExecuteNonQuery() 处出错;

回答by Ponder Stibbons

In order to execute more than one command put them in begin ... end;block. And for DDL statements (like create table) run them with execute immediate. This code worked for me:

为了执行多个命令,将它们放在begin ... end;块中。对于 DDL 语句(如create table),使用execute immediate. 这段代码对我有用:

OracleConnection con = new OracleConnection(connectionString);
con.Open();

OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandText =
    "begin " +
    "  execute immediate 'create table test1(name varchar2(50) not null)';" +
    "  execute immediate 'create table test2(name varchar2(50) not null)';" +
    "end;";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();

More info: Executing SQL Scripts with Oracle.ODP

更多信息:使用 Oracle.ODP 执行 SQL 脚本

回答by BugFinder

Have you tried

你有没有尝试过

cmd.CommandText = "create table test(name varchar2(50) not null);"+"create table test2(name varchar2(50) not null);";