gpt4 book ai didi

c# - 为什么 C# 时分号不能放在 OracleCommand 的 CommandText 中

转载 作者:太空狗 更新时间:2023-10-30 00:27:32 26 4
gpt4 key购买 nike

为什么在 C#

时, 分号 (';') 不能放在 OracleCommand 的 CommandText

如下所示:

string sql = "select * from table1;";
OracleCommand oc = new OracleCommand(sql , con);
oc.CommandType = CommandType.Text;
oc.ExecuteNonQuery();

结果将是一个错误。

为什么?谁能告诉我原因吗?

最佳答案

在评论中,我们已经为您找到了答案。只是为了让你得到一个官方的“答案”,这里是来自 OracleOverflow 站点(见下文):

An OracleCommmand can run only 1 sql or plsql statement at a time. There is no need of separating multiple statements.

The semicolon is part of documentation to divide multiple statements. It is not part of the single DDL statement. For an OracleCommand there is no need to append a semicolon because it can only handle one statement at a time.

编辑:这是原来的链接,现已失效:http://www.oracleoverflow.com/questions/145/running-ddl-through-an-oraclecommand-without-the-semicolon

备选方案:http://web.archive.org/web/20100806014022/http://www.oracleoverflow.com/questions/145/running-ddl-through-an-oraclecommand-without-the-semicolon

问题:

When trying to run a series of DDL commands through an OracleCommand object, I get an ORA-00911 'invalid character' error if I include the final semicolon, even though this is part of the SQL syntax in the documentation. It also means that you can only execute SQL one statement at a time, rather than executing a whole SQL script at once, which seems very odd to me.

After googling for a bit, I found that a workaround is to wrap the DDL in an EXECUTE IMMEDIATE PL/SQL statement, but again it fails if the DDL has a semicolon at the end (apart from PL/SQL create statements, which require the semicolon).

Does anyone know the reason for this behaviour? The semicolon is in the syntax documentation, so I would have assumed it is always required, but it fails if the semicolon is included...

在这里回答:

An OracleCommmand can run only 1 sql or plsql statement at a time. There is no need of separating multiple statements.

The semicolon is part of documentation to divide multiple statements. It is not part of the single DDL statement. For an OracleCommand there is no need to append a semicolon because it can only handle one statement at a time.

ADDED: You can bundle more than one statement into an BEGIN ... END; Block (yes, this ends with semicolon). But this statements should be DML, DDL or PL/SQL function calls. You cannot bundle multiple SELECT statements into an BEGIN ... END; Block.

评论:

为什么 OracleCommand 有一次只能运行 1 个语句的限制?

它基于 ADO.Net 中的 DbCommand 规范。这不仅仅是 OracleCommand 的限制。

SqlCommand 允许在单个命令中使用多个语句,这必须只是 SQL Server 的“添加”。

关于c# - 为什么 C# 时分号不能放在 OracleCommand 的 CommandText 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6212575/

26 4 0