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

查看:66
本文介绍了在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),请使用execute immediate运行它们.这段代码对我有用:

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天全站免登陆