在 Oracle 中的一个 ExecuteScalar 中运行多个命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/685850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run multiple commands in one ExecuteScalar in Oracle
提问by Konstantinos
I have a batch of sql statements such as ...
我有一批sql语句,例如...
insert into.... ; insert into.... ; delete .........;
插入.... ; 插入.... ; 删除 .........;
etc
等等
When i try to execute them against oracle it gives me thiserror (ORA-00911 Invalid Character)
当我尝试对 oracle 执行它们时,它给了我这个错误(ORA-00911 无效字符)
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.
现在我可以理解这是因为语句之间的分号,我在 SQL Server 上尝试了这个并且它有效,但在 Oracle 中到目前为止还没有运气。
Is there a way to run multiple statements against oracle by using the ExecuteScalar or some other function?
有没有办法通过使用 ExecuteScalar 或其他一些函数来针对 oracle 运行多个语句?
DUPLICATE: How can I execute multiple Oracle SQL statements with .NET
回答by Mitch Wheat
Try wrapping with a BEGIN..END
尝试用一个包裹 BEGIN..END
BEGIN insert into.... ; insert into.... ; delete .........; END;
回答by Konstantinos
trying the BEGIN END did not work for me.
尝试 BEGIN END 对我不起作用。
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 thispost after Petrostold 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.
PS,您可能需要根据自己的需要更改它,在我的情况下,我需要打开连接,并在调用者发生某些事情时相应地关闭连接。