【问题标题】:Execute multiple queries in single Oracle command in C#在 C# 中的单个 Oracle 命令中执行多个查询
【发布时间】:2015-11-02 05:37:23
【问题描述】:

我正在使用 Visual Studio 2013 和 oracle 数据库。我想在单个 oracle 命令中一次执行多个创建表查询,这可能吗?我正在尝试关注但不工作

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() 处出错;

【问题讨论】:

    标签: c# oracle visual-studio plsql


    【解决方案1】:

    为了执行多个命令,请将它们放在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();
    

    更多信息:Executing SQL Scripts with Oracle.ODP

    【讨论】:

    • 第二个查询的第一列的索引是多少? textbox1.Text = oraReder.GetString(index).ToString();
    • 更多信息链接不可用。
    【解决方案2】:

    你试过了吗

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

    【讨论】:

    • 您可能需要一个开始和结束语句。 “不起作用”没有帮助。
    • 尝试使用 begin 和 end 语句但同样的错误。 @BugFinder
    • 在 ExecuteNonQuery() 处出现错误消息“Oracle.DataAccess.dll 中发生类型为 'Oracle.DataAccess.Client.OracleException' 的未处理异常”; @BugFinder
    • @BugFinder 第二个查询的第一列的索引是多少? textbox1.Text = oraReder.GetString(index).ToString();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2010-09-19
    • 2019-06-30
    • 1970-01-01
    相关资源
    最近更新 更多