从C#代码后面调用存储过程时插入语句不起作用

时间:2013-03-31 13:46:26

标签: asp.net sql stored-procedures plsql oracle10g

我的数据库中有一个存储过程,它将数据插入到3个表中。该过程已正确编译。我用PL / SQL DEVELOPER工具测试了它,带有样本数据&它工作正常。

在存储过程的代码中,我使用了一个变量successCnt1来计算no。成功的插入语句。然后将此变量分配给successCnt。我从后面的代码中获取此变量的值。如果successCnt == 0则显示错误。

问题当我从C#代码调用它后面的程序返回successCnt == 0.请告诉我哪里做错了!!提前谢谢。

表架构:

TBLTRAIN架构: {TRAINID(VARCHAR2(5)),DATE1(DATE),AC2SEAT(NUMBER),AC3SEAT(NUMBER),SLEEPERSEAT(NUMBER),ACCHAIRCARSEAT(NUMBER),SECONDSEAT(NUMBER) )}

TBLTRAINFARE架构: {TRAINID(VARCHAR2(5)),CLASS(VARCHAR2(5)),FARE(NUMBER)}

TBLTRAINDETAIL架构: {TRAINID(VARCHAR2(5)),FROM1(VARCHAR2(15)),TO1(VARCHAR2(15)),TRAINNAME(VARCHAR2(15))}

程序:

CREATE OR REPLACE PROCEDURE TrainInsertByParameter (p_ac2seat   in number,
                                       p_ac3seat  in number,
                                       p_ccseat  in number,
                                       p_sleeperseat  in number,
                                       p_secondseat  in number,
                                       p_date1      in date,
                                       p_trainid   in varchar2,
                                       p_fare in number,
                                       p_fromplace in varchar2,
                                       p_toplace in varchar2,
                                       p_class in varchar2,
                                       p_trainname in varchar2,
                                       p_successCnt  out number)  IS
successCnt1 number(6) NOT NULL := 0;
rowUpdated1 number(6) NOT NULL := 0;
rowUpdated2 number(6) NOT NULL := 0;
rowUpdated3 number(6) NOT NULL := 0;

BEGIN
  SAVEPOINT before;

  INSERT INTO tbltrain (ac2seat,ac3seat,acchaircarseat,sleeperseat,secondseat,date1,trainid) VALUES(p_ac2seat,p_ac3seat,p_ccseat,p_sleeperseat,p_secondseat,p_date1,p_trainid);


rowUpdated1 := SQL%RowCount;
successCnt1 := successCnt1 + 1;

   dbms_output.put_line('Successful Insertion tbltrain. count ='||successCnt1);
   dbms_output.put_line('Successful Insertion tbltrain. Row Updated ='||rowUpdated1);

  INSERT INTO tbltraindetail (trainid,trainname,from1,to1) VALUES(p_trainid,p_trainname,p_fromplace,p_toplace);


rowUpdated2 := SQL%RowCount;
successCnt1 := successCnt1 + 1;

 dbms_output.put_line('Successful Insertion tbltraindetail. count ='||successCnt1);
 dbms_output.put_line('Successful Insertion tbltraindetail. Row Updated= '||rowUpdated2);

INSERT INTO tbltrainfare(trainid,class,fare) VALUES (p_trainid,p_class,p_fare);

rowUpdated3 := SQL%RowCount;
successCnt1 := successCnt1 + 1;
p_successCnt := successCnt1;

 COMMIT;

 dbms_output.put_line('Successful Insertion tbltrainfare. count ='||p_successCnt);
 dbms_output.put_line('Successful Insertion tbltrainfare. Row Updated= '||rowUpdated3);

  EXCEPTION
    WHEN Others THEN
      successCnt1 := 0;
      p_successCnt := successCnt1;
      dbms_output.put_line('An error has occured. count ='||p_successCnt);

    ROLLBACK TO before;

END;

调用C#代码后面的代码(使用Microsoft Visual Studio))

OracleConnection con1 = new OracleConnection();
                 con1.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionStringOracle"].ConnectionString;

            Int32 fare = Convert.ToInt32(txtFare.Value);
            String date1 = txtDate.Value.ToString();
            String class1 = ddlClass.SelectedItem.Text.ToString();
            String fromplace = txtFromPlace.Text.ToString();
            String toplace = txtToPlace.Text.ToString();
            String trainid = txtId1.Text.ToString();
            String trainname = txtTrainName.Text.ToString();
            Int32 ac2seat = Convert.ToInt32(txtAc2TierSeat.Value);
            Int32 ac3seat = Convert.ToInt32(txtAc3TierSeat.Value);
            Int32 ccseat = Convert.ToInt32(txtAcChairCarSeat.Value);
            Int32 sleeperseat = Convert.ToInt32(txtSleeperSeat.Value);
            Int32 secondseat = Convert.ToInt32(txtSecondSeat.Value);
            Int32 SuccessCnt;

 OracleCommand cmd = new OracleCommand();
                    cmd.Connection = con1;
                    cmd.CommandText = "TrainInsertByParameter";
                    cmd.CommandType = CommandType.StoredProcedure;


                    cmd.Parameters.Add("p_ac2seat", OracleType.Number).Value = ac2seat;
                    cmd.Parameters.Add("p_ac3seat", OracleType.Number).Value = ac3seat;
                    cmd.Parameters.Add("p_ccseat", OracleType.Number).Value = ccseat;
                    cmd.Parameters.Add("p_sleeperseat", OracleType.Number).Value = sleeperseat;
                    cmd.Parameters.Add("p_secondseat", OracleType.Number).Value = secondseat;
                    cmd.Parameters.Add("p_date1", OracleType.DateTime).Value = date1;
                    cmd.Parameters.Add("p_trainid", OracleType.VarChar).Value = trainid;
                    cmd.Parameters.Add("p_fare", OracleType.Number).Value = fare;
                    cmd.Parameters.Add("p_fromplace", OracleType.VarChar).Value = fromplace;
                    cmd.Parameters.Add("p_toplace", OracleType.VarChar).Value = toplace;
                    cmd.Parameters.Add("p_class", OracleType.VarChar).Value = class1;
                    cmd.Parameters.Add("p_trainname", OracleType.VarChar).Value = trainname;


                    cmd.Parameters.Add("p_successCnt", OracleType.Number).Direction = ParameterDirection.Output;


                    try
                    {


                        cmd.ExecuteNonQuery();
                        SuccessCnt = Convert.ToInt32(cmd.Parameters["p_successCnt"].Value);

                        if (SuccessCnt == 0)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "Window", "alert('An error has occured. No data has been inserted. Please try again with valid data');", true);

                        }
                        else if (SuccessCnt > 0)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "Window", "alert('Data has been inserted successfully.');", true);
                        }

                    }
                    catch (Exception ex)
                    {

                    }

我的问题在这里: 在我调试时,在if-else块中(在try {}块内)控件总是转到if(SuccessCnt = = 0){}部分,因为该过程返回p_successCnt == 0。我期待控制的是什么 否则if(SuccessCnt> 0){}阻塞,因为该过程应该返回p_successCnt == 3

2 个答案:

答案 0 :(得分:0)

我自己没有亲自完成,但我怀疑你必须设置参数大小:

// cmd.Parameters.Add("p_successCnt", OracleType.Number).Direction = ParameterDirection.Output;
OracleParameter count = new OracleParameter("p_successCnt", OracleType.Number);
count.Direction = ParameterDirection.Output;
count.Size = 8;
cmd.Parameters.Add(count);

另请参阅:Oracle Store Procedure does not return any thing in the out parameter

答案 1 :(得分:0)

我建议在您的过程中添加输出参数,以便返回由执行过程时发生的任何异常导致的SQLCODE和SQLERRM值。这样,您可以通过检查SQLCODE = 0来检查是否成功完成。建议的更改标有-- added注释:

CREATE OR REPLACE PROCEDURE TrainInsertByParameter (p_ac2seat   in number,
                                       p_ac3seat  in number,
                                       p_ccseat  in number,
                                       p_sleeperseat  in number,
                                       p_secondseat  in number,
                                       p_date1      in date,
                                       p_trainid   in varchar2,
                                       p_fare in number,
                                       p_fromplace in varchar2,
                                       p_toplace in varchar2,
                                       p_class in varchar2,
                                       p_trainname in varchar2,
                                       p_successCnt  out number,
                                       p_sqlcode out number,    -- added
                                       p_sqlerrm out varchar2)  -- added
IS
  successCnt1 number(6) NOT NULL := 0;
  rowUpdated1 number(6) NOT NULL := 0;
  rowUpdated2 number(6) NOT NULL := 0;
  rowUpdated3 number(6) NOT NULL := 0;  
BEGIN
  SAVEPOINT before;

  INSERT INTO tbltrain
    (ac2seat,ac3seat,acchaircarseat,sleeperseat,secondseat,date1,trainid)
  VALUES
    (p_ac2seat,p_ac3seat,p_ccseat,p_sleeperseat,p_secondseat,p_date1,p_trainid);


  rowUpdated1 := SQL%RowCount;
  successCnt1 := successCnt1 + 1;

  dbms_output.put_line('Successful Insertion tbltrain. count ='||successCnt1);
  dbms_output.put_line('Successful Insertion tbltrain. Row Updated ='||rowUpdated1);

  INSERT INTO tbltraindetail
    (trainid,trainname,from1,to1)
  VALUES
    (p_trainid,p_trainname,p_fromplace,p_toplace);

  rowUpdated2 := SQL%RowCount;
  successCnt1 := successCnt1 + 1;

  dbms_output.put_line('Successful Insertion tbltraindetail. count ='||successCnt1);
  dbms_output.put_line('Successful Insertion tbltraindetail. Row Updated= ' || rowUpdated2);

  INSERT INTO tbltrainfare(trainid,class,fare) VALUES (p_trainid,p_class,p_fare);

  rowUpdated3 := SQL%RowCount;
  successCnt1 := successCnt1 + 1;
  p_successCnt := successCnt1;

  COMMIT;

  dbms_output.put_line('Successful Insertion tbltrainfare. count ='||p_successCnt);
  dbms_output.put_line('Successful Insertion tbltrainfare. Row Updated= '||rowUpdated3);

  p_sqlcode := 0;     -- added
  p_sqlerrm := NULL;  -- added
EXCEPTION
  WHEN Others THEN
    p_sqlcode := SQLCODE;  -- added
    p_sqlerrm := SQLERRM;  -- added

    DBMS_OUTPUT.PUT_LINE('Exception: ' || p_sqlcode || ' ' || SQLERRM);  -- added

    successCnt1 := 0;
    p_successCnt := successCnt1;
    dbms_output.put_line('An error has occured. count ='||p_successCnt);

    ROLLBACK TO before;    
END;

IMO从任何从另一个系统调用的PL / SQL过程返回SQLCODE和SQLERRM是一个好主意,因为它为您提供了一种确定错误是否已发生的一致方法,并告诉您错误是什么。 YMMV。

分享并享受。