在 Oracle 的一个 ExecuteScalar 中运行多个命令

发布于 2024-07-16 01:51:15 字数 523 浏览 13 评论 0原文

我有一批sql语句,例如...

insert into...; 插入.... ; 删除 .........;

当我尝试针对oracle执行它们时,它给了我 this 错误(ORA-00911无效字符)

现在我可以理解这是因为语句之间的分号,我在 SQL Server 上尝试过并且它有效,但在 Oracle 中到目前为止还没有运气。

有没有办法通过使用 ExecuteScalar 或其他函数来针对 Oracle 运行多个语句?


重复我该如何使用.NET执行多个Oracle SQL语句

I have a batch of sql statements such as ...

insert into.... ;
insert into.... ;
delete .........;

etc

When i try to execute them against oracle it gives me this error (ORA-00911 Invalid Character)

now i can understand that this is because of the semicolon between the statements, i tried this on SQL Server and it worked but in Oracle no luck so far.

Is there a way to run multiple statements against oracle by using the ExecuteScalar or some other function?


DUPLICATE: How can I execute multiple Oracle SQL statements with .NET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一页 2024-07-23 01:51:15

尝试用 BEGIN..END 包装

BEGIN insert into.... ; insert into.... ; delete .........; END;

Try wrapping with a BEGIN..END

BEGIN insert into.... ; insert into.... ; delete .........; END;
行雁书 2024-07-23 01:51:15

尝试 BEGIN END 对我来说不起作用。

我所做的是创建一个新方法,给定一个连接(我尝试最小化我的打开连接),它使用 ; 分割语句。 作为分隔符并单独运行每个

    private void ExecuteSql(string statements, IDbConnection conn)
    {
        IDbCommand cmd = conn.CreateCommand();
        string[] commands = statements.Split(new string[] { ";\r\n", "; ", ";\t", ";\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string c in commands)
        {
            cmd.CommandText = c;
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }

        cmd.Dispose();
    }

我的灵感来自 这篇Petros告诉我之后发布关于它

PS,您可能需要根据您的需要更改它,在我的情况下,我需要打开连接,并在调用者发生某些情况时相应地关闭连接。

trying the BEGIN END did not work for me.

What i did was make a new method that given a connection(i try to minimize my open connections) it splits the statements using the ; as a delimiter and runs each one seperatly

    private void ExecuteSql(string statements, IDbConnection conn)
    {
        IDbCommand cmd = conn.CreateCommand();
        string[] commands = statements.Split(new string[] { ";\r\n", "; ", ";\t", ";\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string c in commands)
        {
            cmd.CommandText = c;
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }

        cmd.Dispose();
    }

My inspiration came from this post after Petros told me about it

PS you may need to change it according to your needs, in my case i require the connection to be open, and closed accordingly if something happens from the caller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文