我正在使用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();
cmd.ExecuteNonQuery();
出错答案 0 :(得分:9)
为了执行多个命令,将它们放在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();
答案 1 :(得分:1)
你试过吗
cmd.CommandText = "create table test(name varchar2(50) not null);"+"create table test2(name varchar2(50) not null);";