11

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

Got error at cmd.ExecuteNonQuery();

0

2 Answers 2

21

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

More info: Executing SQL Scripts with Oracle.ODP

Sign up to request clarification or add additional context in comments.

3 Comments

what would be the index of the first column of second query ? textbox1.Text = oraReder.GetString(index).ToString();
More info link is not available.
More info link from web.archive.org
0

Have you tried

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

4 Comments

you might need a begin and end statement. "doesnt work" isnt helpful.
Tried with begin and end statement but same error. @BugFinder
Error message "An unhandled exception of type 'Oracle.DataAccess.Client.OracleException' occurred in Oracle.DataAccess.dll " at ExecuteNonQuery(); @BugFinder
@BugFinder what would be the index of the first column of second query ? textbox1.Text = oraReder.GetString(index).ToString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.