在 C# 中的单个 Oracle 命令中执行多个查询 [英] Execute multiple queries in single Oracle command in C#

查看:34
本文介绍了在 C# 中的单个 Oracle 命令中执行多个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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();

cmd.ExecuteNonQuery() 出错;

Got error at cmd.ExecuteNonQuery();

推荐答案

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

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:

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();

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

这篇关于在 C# 中的单个 Oracle 命令中执行多个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆