【问题标题】:How to execute a block statement in C# for Firebird如何在 C# 中为 Firebird 执行块语句
【发布时间】:2013-04-23 23:23:16
【问题描述】:

我正在尝试使用以下命令删除 Firebird 中数据库中的所有表:

string dropAllForeignKeysSql =
  "EXECUTE BLOCK RETURNS (stmt VARCHAR(1000)) AS "+
  "BEGIN "+
  "FOR "+
  "select 'alter table \"'||r.rdb$relation_name ||'\" drop constraint '||r.rdb$constraint_name||';' "+
  "from rdb$relation_constraints r "+
  "where (r.rdb$constraint_type='FOREIGN KEY') "+
  "into :stmt "+
  " DO begin execute statement :stmt; end "+
  "END "+
  ";";

using (var connection = sessionFactory.OpenSession().Connection)
  {
    var command = connection.CreateCommand();
    command.CommandText = dropAllForeignKeysSql;
    command.ExecuteNonQuery();
  }

很遗憾,该命令未执行。但是,当我暂停调试器时,复制 CommandText 变量中的字符串并手动执行查询/而不是在代码中执行该语句。如果我用更简单的命令(如DROP TABLE myTable)替换 CommandText,则此命令将在代码中执行,即删除表。运行代码时不会抛出错误(与我故意输入错误命令或在外部工具中打开数据库时相比)。

从那里,我得出结论,错误不在 SQL 语句中,也不在命令设置中,也不是在设置错误的连接中。错误还可能在哪里?

【问题讨论】:

  • 不是 100% 确定,但我认为您需要在动态构造的查询末尾删除分号 (;)。分号不是 Firebird 查询语言的一部分。
  • 不幸的是,这不是原因。执行生成的查询字符串工作正常(带和不带分号)。
  • 您是否尝试在每个连接的行中添加换行符?有什么方法可以跟踪数据库服务器上 ExecuteNonQuery 执行的实际 sql 查询?
  • 可以,但这对结果没有影响。
  • 问题也可能与列rdb$relation_name 是一个CHAR(31) 并因此用空格填充的事实有关。尝试使用TRIM

标签: c# sql firebird


【解决方案1】:

你应该使用 FbBatchExecution

FirebirdSql.Data.FirebirdClient.FbTransaction fbt = fbc.BeginTransaction(); // object fbc is your FirebirdSql.Data.FirebirdClient.FbConnection

FirebirdSql.Data.Isql.FbBatchExecution fbe = new FirebirdSql.Data.Isql.FbBatchExecution(fbc);
fbe.SqlStatements.Add(dropAllForeignKeysSql); // Your string here

fbe.Execute(true);

fbt.Commit();

并在开始脚本之前更改“术语”:

SET term #;

【讨论】:

  • 当我执行此操作时,我收到错误The type of the SQL statement could not be determinated. 删除 SET TERM 语句导致Batch execution aborted The returned message was: Execute requires the Command object to have a Transaction object when the Connection object assigned to the command is in a pending local transaction. The Transaction property of the Command has not been initialized. 此消息奇怪地消失了,并且代码在省略事务时运行良好,但在这种情况下,约束再次不会被删除。
  • 什么是 Firebird 引擎版本?
  • Firebird 客户端 3.0.2.1 for Ado.NET,Firebird 2.5.2.26539-0 Win32 作为嵌入式引擎,Firebird 2.5.2.26539 Win32 使用服务器版本时
  • @Dani:还记得上面交易错误信息的原因吗?我在这里遇到完全相同的问题。 (2.5.3.26778.ds4-5)
  • @lenniep 恐怕,不;我们放弃了语句方法:-|
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-13
相关资源
最近更新 更多