OracleCommand命令,ExecuteNonQuery问题

发布于 2024-10-25 08:00:31 字数 2025 浏览 2 评论 0原文

我必须清除 oracle 数据库中的某些表,但是当我运行以下代码时遇到问题时,

public static void ClearDataTables(IList<string> tableNames)
        {
            string connectionString = "CONNECTIONSTRING";
            using (OracleConnection connection = new OracleConnection())
            {
                connection.ConnectionString = connectionString;
                connection.Open();
                foreach (string table in tableNames)
                {
                    OracleCommand command = connection.CreateCommand();
                    string sql = String.Format("DELETE FROM TOA_REPORTING.{0}", table);
                    command.CommandText = sql;
                    command.ExecuteNonQuery();
                }
                connection.Close();
            }
        }

我使用此列表调用此方法

ClearDataTables(new List<string> { "GROUP_DEFINITION", "GROUP_REPORT_EMAIL_LIST", "GROUP_EQUIPMENT_GROUP_STN_XREF"});

它可以很好地运行前两个表,但是在第三个表上,它会卡住并且应用程序永远运行...

有趣的是,当我切换“GROUP_REPORT_EMAIL_LIST”和“GROUP_EQUIPMENT_GROUP_STN_XREF”时 应用程序在遇到第二个表名后将永远运行。

所以总而言之,当函数遇到“GROUP_EQUIPMENT_GROUP_STN_XREF”时,该函数将永远运行。我通过在 Toad 上进行测试来验证生成的 SQL 是否有效。

还有其他人遇到过这个问题吗?

编辑-前两个表在运行时确实会被清除。

解决方案

string connectionString = "CONNECTIONSTRING";
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                connection.Open();
                OracleCommand command = connection.CreateCommand();
                OracleTransaction trans = connection.BeginTransaction();
                command.Transaction = trans;
                foreach (string table in tableNames)
                {
                    string sql = String.Format("DELETE FROM TOA_REPORTING.{0}", table);
                    command.CommandText = sql;
                    command.ExecuteNonQuery();
                }
                trans.Commit();
            }

TRUNCATE 本来是一个非常好的解决方案,但我没有这样做的权限!

I have to clear certain tables in the oracle database however when I'm having issues with running the following code

public static void ClearDataTables(IList<string> tableNames)
        {
            string connectionString = "CONNECTIONSTRING";
            using (OracleConnection connection = new OracleConnection())
            {
                connection.ConnectionString = connectionString;
                connection.Open();
                foreach (string table in tableNames)
                {
                    OracleCommand command = connection.CreateCommand();
                    string sql = String.Format("DELETE FROM TOA_REPORTING.{0}", table);
                    command.CommandText = sql;
                    command.ExecuteNonQuery();
                }
                connection.Close();
            }
        }

I am calling this method with this list

ClearDataTables(new List<string> { "GROUP_DEFINITION", "GROUP_REPORT_EMAIL_LIST", "GROUP_EQUIPMENT_GROUP_STN_XREF"});

It runs the first two tables fine, however on the third one, it gets stuck and the application runs forever...

Funny thing is, when I switch "GROUP_REPORT_EMAIL_LIST" and "GROUP_EQUIPMENT_GROUP_STN_XREF"
The application runs forever after the it hits the second table name.

So in conclusion, the function runs forever when it hits "GROUP_EQUIPMENT_GROUP_STN_XREF". I've verified that the SQL generated works by testing it out on toad.

Anyone else ran into this issue?

EDIT - The first two tables does indeed get cleared when it runs.

Solution

string connectionString = "CONNECTIONSTRING";
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                connection.Open();
                OracleCommand command = connection.CreateCommand();
                OracleTransaction trans = connection.BeginTransaction();
                command.Transaction = trans;
                foreach (string table in tableNames)
                {
                    string sql = String.Format("DELETE FROM TOA_REPORTING.{0}", table);
                    command.CommandText = sql;
                    command.ExecuteNonQuery();
                }
                trans.Commit();
            }

TRUNCATE would have been a very nice solution, however I do not have the privileges to do so!

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

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

发布评论

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

评论(4

锦欢 2024-11-01 08:00:31

您是否忘记在 Toad(或任何其他客户端)中提交更改?开放的事务将导致它无限期地等待。

Have you forgotten to commit your changes in Toad (or any other client)? An open transaction will cause it to wait indefinitely.

高速公鹿 2024-11-01 08:00:31

该表中有很多数据吗?这可以解释为什么删除数据需要这么长时间。
无论如何,我建议使用 TRUNC 来清除表。

Is there a lot of data in that table? This would explain, why it takes so long to delete the data.
Anyway, I suggest to use TRUNC for clearing tables.

拒绝两难 2024-11-01 08:00:31

大量删除可能会非常慢,尤其是在一个事务中运行它们时。如果您根本不需要该事务,请使用:

truncate table YourTable

如果需要,请将删除拆分为小型事务。基本上运行:

delete from YourTable where rownum < 100

直到表为空。例如,请参阅此博客文章

Large number of deletes can be very slow, especially if you run them in one transaction. If you don't need the transaction at all, use:

truncate table YourTable

If you do, split the delete over small-sized transactions. Basically run:

delete from YourTable where rownum < 100

until the table is empty. See for example this blog post.

梦幻的心爱 2024-11-01 08:00:31

我可能会编写一个存储过程来执行所有删除或截断并调用 SP 一次,而不是在客户端循环。

编辑:最好不要在循环内创建命令对象。使用表名参数在循环外部创建它一次,然后调用它,并在每次迭代时为其提供不同的参数值。但SP是首选。

I would probably write a stored procedure that does all of the deletions or truncations and invoke the SP once, rather than have a loop client-side.

EDIT: It would also be better not to create the command object inside the loop. Create it once outside the loop with a table-name parameter, and then invoke it feeding it a different parameter value with each iteration. But the SP is to be preferred.

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