【问题标题】:Unable to execute multiple Oracle queries in C#无法在 C# 中执行多个 Oracle 查询
【发布时间】:2019-06-13 10:56:22
【问题描述】:

在下面的代码中,当我尝试更新表 t_payment 中的金额时,我希望它被设置为 Convert.ToInt32(request.amount+ previous_paid_amount); 的值,而 previous_paid_amount 被假定为 0 而不是更新。所以我无法使用previous_paid_amount 变量的更新值。

任何帮助将不胜感激。 谢谢

double previous_paid_amount = 0;
try {
  OracleCommand command2 = new OracleCommand();

  command2.CommandText = "select amount from t_payment where penalty_order_id = (select id from t_penalty_order where protokol_no = :invoiceNumber)";

  command2.Parameters.Add(new OracleParameter(@"invoiceNumber", OracleDbType.Varchar2, 255)).Value = request.invoiceNumber;

  command2.Connection = connection;
  command2.CommandType = System.Data.CommandType.Text;

  using (OracleDataReader row2 = command.ExecuteReader()) {
    while (row2.Read()) {
      previous_paid_amount = Convert.ToInt32(row2.GetValue(0));
    }
  }
}
catch (Exception e) {
  completePayment.code = 111;
  completePayment.message = e.Message;
  completePayment.transactionNumber = null;
}

// update the paid amount by adding the current amount
try {
  OracleCommand command2 = new OracleCommand();

  command2.CommandText = "Update t_payment set amount = :amount where penalty_order_id = (select id from t_penalty_order where protokol_no = :invoiceNumber)";

  command2.Parameters.Add(new OracleParameter(@"amount", OracleDbType.Int32)).Value = Convert.ToInt32(request.amount+ previous_paid_amount);
  command2.Parameters.Add(new OracleParameter(@"invoiceNumber", OracleDbType.Varchar2, 255)).Value = request.invoiceNumber;

  command2.Connection = connection;
  command2.CommandType = System.Data.CommandType.Text;
  command2.ExecuteNonQuery();
}
catch (Exception e) {
  completePayment.code = 111;
  completePayment.message = e.Message;
  completePayment.transactionNumber = null;
}

【问题讨论】:

  • 有些奇怪。 previous_paid_amount 被声明为 double,但分配为 int "previous_paid_amount = Convert.ToInt32(row2.GetValue(0))" 然后随着 request.amount (typeof?) 递增,总和重新转换为 int

标签: c# sql oracle


【解决方案1】:

我建议只执行 一个 查询(我们不想通过查询 Oracle 两次来做额外的工作,将数据提取到工作站然后再次向 Oracle 推送)。据我所知,您的

"Update t_payment set amount = :amount where penalty_order_id = (select id from t_penalty_order where protokol_no = :invoiceNumber)";

代码,您想更新t_payment 表,方法是向amount 字段添加一些额外的钱 并应用条件:

update t_payment
   set amount = amount + SOME_EXTRA_MONEY
 where penalty_order_id in (select p.id 
                              from t_penalty_order p
                             where p.protokol_no = :invoiceNumber) 

我们必须确定SOME_EXTRA_MONEY 是什么:我们可以尝试从查询中推导出它

"select amount from t_payment where penalty_order_id = (select id from t_penalty_order where protokol_no = :invoiceNumber)";

所以我们有(我添加了Sum,以防我们有几条记录,这就是为什么应该对它们求和,Nvl,以防我们有 - 在这种情况下,额外的总和是0):

update t_payment
   set amount = amount + (select Nvl(Sum(t.amount), 0) 
                            from t_payment t 
                           where t.penalty_order_id in (select p.id 
                                                          from t_penalty_order p
                                                         where p.protokol_no = :invoiceNumber))
 where penalty_order_id in (select p.id 
                              from t_penalty_order p
                             where p.protokol_no = :invoiceNumber)

执行此查询的时间:

 using (OracleCommand command2 = new OracleCommand()) {
   command2.CommandText =
     @"update t_payment
          set amount = amount + (select Nvl(Sum(t.amount), 0) 
                                   from t_payment t 
                                  where t.penalty_order_id in (select p.id 
                                                                 from t_penalty_order p
                                                                where p.protokol_no = :invoiceNumber))
        where penalty_order_id in (select p.id 
                                     from t_penalty_order p
                                    where p.protokol_no = :invoiceNumber)";

   command2.Parameters.Add(
     new OracleParameter(@"invoiceNumber", OracleDbType.Varchar2, 255)
   ).Value = request.invoiceNumber;

   command2.ExecuteNonQuery();
 } 

【讨论】:

  • 非常感谢德米特里!完美的解决方案!
【解决方案2】:

它不起作用,因为您使用的是 command 而不是 command2。

using (OracleDataReader row2 = command.ExecuteReader())

这需要修改

 using (OracleDataReader row2 = command2.ExecuteReader())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2011-11-10
    • 2019-06-30
    相关资源
    最近更新 更多