简体   繁体   English

为什么语句在Toad中执行而在我的代码中不执行?

[英]Why would a statement execute in Toad but not in my code?

Here is my code: 这是我的代码:

private void UpdatePlatypus(String APetPlatypus) 
{
    oracleConnectionMainForm.Open();
    OracleCommand ocmd = new OracleCommand();
    ocmd.Connection = oracleConnectionMainForm;
    try 
    {
        ocmd.CommandText = @"<Update SQL statement that contains one parameter, like so: "WHERE DUCKBILLEDPLATYPUS = :PLATYPUS">)";
        ocmd.Parameters.Add("PLATYPUS", APetPlatypus);
        ocmd.ExecuteNonQuery();
    } 
    catch (Exception e) 
    {
        MessageBox.Show(String.Format("UpdatePlatypus failed with message {0}", e.Message));
    } 
    finally 
    {
        oracleConnectionMainForm.Close();
    }
    MessageBox.Show(String.Format("UpdatePlatypus to {0} succeeded", APetPlatypus));
}

In Toad, this SQL works fine -- I simply replace ":PLATYPUS" with the value "Phineas" in the Toad SQL editor, and the records are, indeed, updated, as the "27 records affected" message and a subsequent SQL Select that returns the updated records shows. 在Toad中,这个SQL工作正常 - 我只是在Toad SQL编辑器中将值“Phineas”替换为“:PLATYPUS”,并且确实更新了记录,作为“受影响的27个记录”消息和随后的SQL Select返回更新的记录显示。

But in my C# app, it hangs on the call to ExecuteNonQuery()...I never see any of the messages - neither that the Update failed, nor that it succeeded - it simply hangs there, floating about in space like a smurf ball on the moon. 但是在我的C#应用​​程序中,它挂起了对ExecuteNonQuery()的调用...我从来没有看到任何消息 - 更新失败,也没有成功 - 它只是挂在那里,漂浮在空间就像一个蓝色的球在月球上。

UPDATE UPDATE

I copied some old Update code for dotConnect for Oracle that worked, but it still does the same thing (hangs on the call to ExecuteNonQuery() 我复制了一些有用的dotConnect for Oracle的旧更新代码,但它仍然做同样的事情(挂起对ExecuteNonQuery()的调用

private void UpdatePlatypus(String APetPlatypus) {

    OracleCommand ocmd;

    oracleConnectionMainForm.Open();
    String update = @"<same update sql as above>";
    ocmd = new OracleCommand(update, oracleConnectionMainForm);
    ocmd.CommandType = CommandType.Text;
    try {
        OracleParameter p_DuckbilledPlatypus =
        new OracleParameter("DIVISION", OracleDbType.NVarChar, ParameterDirection.Input);
        p_DuckbilledPlatypus.Value = APetPlatypus;
        ocmd.Parameters.Add(p_DuckbilledPlatypus);
        using (var transaction = oracleConnectionMainForm.BeginTransaction()) {
            try {
                ocmd.Transaction = transaction;
                ocmd.ExecuteNonQuery();
                transaction.Commit();
            } catch (Exception ex) {
                transaction.Rollback();
                throw;
            }
        }
    } catch (Exception e) {
        MessageBox.Show(String.Format("UpdatePlatypus failed with message {0}", e.Message));
    } finally {
        oracleConnectionMainForm.Close();
    }
    MessageBox.Show(String.Format("UpdatePlatypus to {0} succeeded", APetPlatypus));
}

ANOTHER UPDATE 另一个更新

Is it possible that the SQL statement can be too confusing for whatever it is that parses it, even though it is valid SQL? 是否有可能SQL语句对于解析它的任何内容都会过于混乱,即使它是有效的SQL?

Again, this query runs fine in Toad, but it is quite complex: it contains TWO nested Select statements, like so: 同样,这个查询运行在蟾蜍很好,但它相当复杂的:它包含两个嵌套的SELECT语句,就像这样:

Update <tableName> 
Set <col = value> 
where <col> in 
(select bla from bla where bla = :Platypus) 
and (bla is null or bla)
and bla in (select distinct bla from bla 
where bla = bla and bla is not null)

YET ANOTHER UPDATE 再来一次更新

If I simplify the query by replacing the sub-selects with parameters (that are supplied via the results returned from those atmoic SQL select statements) it runs (and almost instantaneously, at that). 如果我通过用参数替换子选项来简化查询(通过从那些atmoic SQL select语句返回的结果提供),它就会运行(并且几乎是瞬间完成)。

So, I guess the nested subselects were too much for whatever it is that parses the Update statement... 所以,我猜嵌套的子选择对于解析Update语句的任何东西太过分了......

Did you hit "commit" button in TOAD after executing your UPDATE ? 执行UPDATE后,您是否在TOAD中点击了“提交”按钮? It looks that it hangs simply becacuse of the lock on the updated rows. 看起来它只是因为锁定更新的行而挂起。

BTW: In TOAD, menu Database -> Monitor -> Session Browser, find your "C#" or TOAD oracle sessions and look at the "Locks" tab, there are two sub-tabs "Blocking locks" and "Blocked locks". BTW:在TOAD中,菜单Database - > Monitor - > Session Browser,找到你的“C#”或TOAD oracle会话并查看“Locks”选项卡,有两个子标签“Blocking locks”和“Blocked locks”。 Is there any entry when your session hangs ? 您的会话挂起时是否有任何条目?

After you ExecuteNonQuery() call ExecuteNonQuery again with CommandText COMMIT . ExecuteNonQuery()之后,使用CommandText COMMIT再次调用ExecuteNonQuery Oracle says it documentation that it commit automatically but actually it's not. 甲骨文称其自动提交的文档但实际上并非如此。 It's committing only when you close the connection. 它仅在您关闭连接时提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM