这是我的代码:
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));
}
在 Toad 中,此 SQL 工作正常——我只需在 Toad SQL 编辑器中将“:PLATYPUS”替换为值“Phineas”,并且确实更新了记录,因为“27 个记录受影响”消息和随后的 SQL 选择返回更新的记录显示。
但是在我的 C# 应用程序中,它挂在对 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));
}
另一个更新
是否有可能 SQL 语句对于解析它的任何东西来说都太混乱了,即使它是有效的 SQL 也是如此?
同样,此查询在 Toad 中运行良好,但相当复杂:它包含两个嵌套的 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)
另一个更新
如果我通过将子选择替换为参数(通过从那些 atmoic SQL 选择语句返回的结果提供)来简化查询,它会运行(并且几乎是即时的)。
所以,我猜嵌套的子选择对于解析更新语句的任何东西来说都太多了......