无法在C#中执行多个Oracle查询

时间:2019-06-13 10:56:22

标签: c# sql oracle

在以下代码中,当我尝试更新表t_payment中的金额时,我希望将其设置为Convert.ToInt32(request.amount+ previous_paid_amount);的值,而假定previous_paid_amount0而不是获取更新。因此,我无法使用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;
}

2 个答案:

答案 0 :(得分:1)

我建议只执行一次 查询(我们不想通过查询Oracle 两次,将数据提取到工作站来完成额外工作然后将Agian推向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();
 } 

答案 1 :(得分:0)

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

using (OracleDataReader row2 = command.ExecuteReader())

这需要使用

进行修改
 using (OracleDataReader row2 = command2.ExecuteReader())