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

Han*_*nni 6 c# oracle plsql visual-studio

我正在使用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();
Run Code Online (Sandbox Code Playgroud)

cmd.ExecuteNonQuery();出错

Pon*_*ons 12

为了执行多个命令,将它们放入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();
Run Code Online (Sandbox Code Playgroud)

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

  • 更多信息链接不可用。 (2认同)