卡住了多次调用oracle程序

时间:2013-04-20 00:36:00

标签: c# oracle plsql

无论如何我都可以加快速度。我每次都要调用存储过程从oracle获取最新值并更新。这是我的最后一个瓶颈,因为它可以被称为数千次。我将其他插入转换为数组绑定批量插入。

c#c​​all oracle

for (int i = 0; i < r.receiptkey.Count(); i++)
{       
  ld.receiptlinenumber.Add(AsnGetNextAvailableReceiptLineNumber(r.receiptkey[i]));
}

c#oracle

 public string AsnGetNextAvailableReceiptLineNumber(string myReceiptKey)
    {
        using (OracleCommand cmd = new OracleCommand())
        {
            OracleConnection conn;
            conn = new OracleConnection(ConnectionStringOracle);
            cmd.CommandText = "bpPack.GetNextReceiptLineNumber";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.Add(new OracleParameter("iReceiptKey", OracleDbType.Varchar2, 10));
            cmd.Parameters.Add(new OracleParameter("oRetValue", OracleDbType.Varchar2, 5)).Direction = ParameterDirection.Output;
            cmd.Parameters["iReceiptKey"].Value = myReceiptKey;
            conn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception e9)
            {
                return "Error" + e9.Message;
            }
            return cmd.Parameters["oRetValue"].Value.ToString();
        }
    }

plsql

  PROCEDURE GetNextReceiptLineNumber(
   iReceiptKey IN VARCHAR2,
   oRetValue OUT VARCHAR2)
  IS
    xreceiptlinenumber VARCHAR2 (5);
    xreceiptlinecount  NUMBER (10);
  BEGIN
     xreceiptlinenumber :='';
     xreceiptlinecount  := 0;
     SELECT MAX (receiptlinenumber)
    INTO xreceiptlinenumber
     FROM receiptdetail
    WHERE receiptkey        = iReceiptKey;
   IF (xreceiptlinenumber IS NULL) THEN
    xreceiptlinecount    := 0;
  ELSE
    xreceiptlinecount := TO_NUMBER (LTRIM (xreceiptlinenumber, '0'));
  END IF;
  xreceiptlinecount := xreceiptlinecount + 1;
  oRetValue         := LPAD (xreceiptlinecount, 5, '0');
  END GetNextReceiptLineNumber;

2 个答案:

答案 0 :(得分:0)

不要在每个过程调用上连接 - 单独的连接代码和过程调用代码。

<小时/> 你的程序可以进入获取重复的情况(想象如果两个会话同时使用相同的密钥调用它会发生什么 - 如果可能的话,使用序列而不是max。

答案 1 :(得分:0)

您可以在C#代码中详细说明以下查询检索到的结果。

SELECT receiptkey, LPAD (nvl((to_number(MAX(receiptlinenumber))+1),'0'), 5, '0') as NextReceiptLineNumber
FROM receiptdetail
group by receiptkey