Execute multiple queries in single Oracle command in C#

前端 未结 2 2007
礼貌的吻别
礼貌的吻别 2020-11-29 12:08

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

相关标签:
2条回答
  • 2020-11-29 12:59

    Have you tried

    cmd.CommandText = "create table test(name varchar2(50) not null);"+"create table test2(name varchar2(50) not null);";
    
    0 讨论(0)
  • 2020-11-29 13:10

    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

    0 讨论(0)
提交回复
热议问题