通过Oracle Store过程多次插入Oracle DBMS

时间:2017-09-12 06:01:02

标签: c# oracle stored-procedures transactions insert

我想一次向oracle数据库插入多条记录。我创建了一个插入的存储过程,因为我想插入多个表,一个表主键是另一个表中的外键。 下面是我要插入列表的C#代码

 public void InsertToDatabase(List<EmployeeData> Allemployee)
        {

            CheckConnection();
            OracleTransaction trans = con.BeginTransaction();
            try
            {
                OracleCommand com = new OracleCommand();


                foreach (var Item in Allemployee)
                {
                    com.CommandType = CommandType.StoredProcedure;
                    com.CommandText = "INSERT_WAGES";
                    com.Transaction = trans;
                    com.Parameters.Add(new OracleParameter("YEAR", Item.YEAR));
                    com.Parameters.Add(new OracleParameter("EMPLOYER_TAX_PAYER_NO", Item.EMPLOYER_TAX_PAYER_NO));
                    com.Parameters.Add(new OracleParameter("TAX_PAYER_NO", Item.TAX_PAYER_NO));
                    com.Parameters.Add(new OracleParameter("EMPLOYMENT_NO", Item.EMPLOYMENT_NO));
                    com.Parameters.Add(new OracleParameter("ENTERPRISE_NO", Item.ENTERPRISE_NO));
                    com.Parameters.Add(new OracleParameter("TOTAL_BASE_SALARY", Item.TOTAL_BASE_SALARY));
                    com.Parameters.Add(new OracleParameter("WITHH_DECL_MONTH", Item.WITHH_DECL_MONTH));
                    com.Parameters.Add(new OracleParameter("PAYMENT_DATE", Item.PAYMENT_DATE));
                    com.Parameters.Add(new OracleParameter("AMOUNT", Item.AMOUNT));
                    com.Parameters.Add(new OracleParameter("CALCULATED_TAX", Item.CALCULATED_TAX));
                    com.Parameters.Add(new OracleParameter("TAX_PERIOD_NO", Item.TAX_PERIOD_NO));
                    com.Parameters.Add(new OracleParameter("WDR_TAX_ACCOUNT_NO", Item.WDR_TAX_ACCOUNT_NO));
                    com.Parameters.Add(new OracleParameter("CREDITED_TAX_PERIOD_NO", Item.CREDITED_TAX_PERIOD_NO));
                    com.Parameters.Add(new OracleParameter("WDEE_TAX_ACCOUNT_NO", Item.WDEE_TAX_ACCOUNT_NO));
                    com.Parameters.Add(new OracleParameter("ENTER_USER", Item.ENTER_USER));
                    com.Parameters.Add(new OracleParameter("IRD_EMPLOYEE_NO", Item.IRD_EMPLOYEE_NO));
                    com.Parameters.Add(new OracleParameter("TAX_CENTRE_NO", Item.TAX_CENTRE_NO));
                    com.ExecuteNonQuery();
                }
            }

            catch (Exception e)
            {
                trans.Rollback();
                return;
            }
            trans.Commit();

        }

以下是我在Oracle数据库中的存储过程

create or replace procedure INSERT_WAGES(   
                        P_YEAR   in TAX_WITHHELD.YEAR%TYPE,
                        P_EMPLOYER_TAX_PAYER_NO   in TAX_WITHHELD.EMPLOYER_TAX_PAYER_NO%TYPE,
                        P_TAX_PAYER_NO    in TAX_WITHHELD.TAX_PAYER_NO%TYPE,                       
                        P_EMPLOYMENT_NO in TAX_WITHHELD.EMPLOYMENT_NO%TYPE,
                        P_ENTERPRISE_NO in TAX_WITHHELD.ENTERPRISE_NO%TYPE,
                        P_TOTAL_BASE_SALARY in TAX_WITHHELD.TOTAL_BASE_SALARY%TYPE,
                        P_WITHH_DECL_MONTH in TAX_WITHHELD.WITHH_DECL_MONTH%TYPE,
                        P_PAYMENT_DATE in TAX_WITHHELD.PAYMENT_DATE%TYPE,
                        P_AMOUNT in TAX_WITHHELD_DETAIL.AMOUNT%TYPE,
                        P_CALCULATED_TAX in TAX_WITHHELD_DETAIL.CALCULATED_TAX%TYPE,
                        P_TAX_PERIOD_NO in TAX_WITHHELD_DETAIL.TAX_PERIOD_NO%TYPE,
                        P_WDR_TAX_ACCOUNT_NO in TAX_WITHHELD_DETAIL.WDR_TAX_ACCOUNT_NO%TYPE,
                        P_CREDITED_TAX_PERIOD_NO in TAX_WITHHELD_DETAIL.CREDITED_TAX_PERIOD_NO%TYPE,
                        P_WDEE_TAX_ACCOUNT_NO in TAX_WITHHELD_DETAIL.WDEE_TAX_ACCOUNT_NO%TYPE,
                        P_ENTER_USER in TAX_WITHHELD_DETAIL.ENTER_USER%TYPE,
                        P_IRD_EMPLOYEE_NO in TAX_TRANSACTION.IRD_EMPLOYEE_NO%TYPE,
                        P_TAX_CENTRE_NO in TAX_TRANSACTION.TAX_CENTRE_NO%TYPE
                         ) as
--varaible to get the sequence of all four table and store it in varbile
  V_TAX_WITHHELD_NO TAX_WITHHELD.TAX_WITHHELD_NO%TYPE;
  V_TAX_WITHHELD_DETAIL_NO TAX_WITHHELD_DETAIL.TAX_WITHHELD_DETAIL_NO%TYPE;
  V_TAX_TRANS_NO  TAX_TRANSACTION.TAX_TRANS_NO%TYPE;
  V_TAX_SUB_TRANS_NO TAX_SUB_TRANS.TAX_SUB_TRANS_NO%TYPE;
BEGIN 

 select TAX_WITHHELD_NO_SEQ.NEXTVAL into V_TAX_WITHHELD_NO  from dual;
 select TAX_WITHHELD_DETAIL_NO_SEQ.NEXTVAL into V_TAX_WITHHELD_DETAIL_NO  from dual;
 select TAX_TRANS_NO_SEQ.NEXTVAL into V_TAX_TRANS_NO  from dual;
 select TAX_TRANS_TYPE_NO_SEQ.NEXTVAL into V_TAX_SUB_TRANS_NO  from dual;
       insert into TAX_WITHHELD 
                       (
                        TAX_WITHHELD_NO,
                        YEAR,
                        TAX_PAYER_NO,
                        EMPLOYER_TAX_PAYER_NO,
                        DATE_ENTERED,
                        EMPLOYMENT_NO,                
                        ENTERPRISE_NO,
                        TOTAL_BASE_SALARY,
                        MAR_STAT_NO,
                        CALCULATED_FAMILY_DIVIDER,
                        WITHH_DECL_MONTH,
                        CAPTURE_TAB,    
                        PAYMENT_DATE
                        )                     
                  Values (V_TAX_WITHHELD_NO,                   
                        P_YEAR,
                        P_TAX_PAYER_NO ,
                        P_EMPLOYER_TAX_PAYER_NO,
                        sysdate,    
                        P_EMPLOYMENT_NO,
                        P_ENTERPRISE_NO,
                        P_TOTAL_BASE_SALARY ,
                        2,
                        1 ,
                        P_WITHH_DECL_MONTH ,
                        72
                        ,                        
                        P_PAYMENT_DATE 

);  
                  insert into TAX_WITHHELD_DETAIL
                       ( TAX_WITHHELD_DETAIL_NO,
                            TAX_WITHHELD_NO,
                            AMOUNT,
                            TAX_TYPE_NO,
                            TAX_PAYER_NO,
                            CALCULATED_TAX,
                            TAX_PERIOD_NO,
                          WDR_TAX_ACCOUNT_NO,
                            CREDITED_TAX_TYPE_NO,
                            CREDITED_TAX_PERIOD_NO,
                            WDEE_TAX_ACCOUNT_NO,
                            TAXABLE_INCOME,
                            ENTER_USER,
                            ENTER_DATE


                        )
                        Values 
                        (
                        V_TAX_WITHHELD_DETAIL_NO,
                        V_TAX_WITHHELD_NO,                   
                        P_AMOUNT,
                          1,
                        P_TAX_PAYER_NO,
                        P_CALCULATED_TAX,
                        P_TAX_PERIOD_NO,
                        P_WDR_TAX_ACCOUNT_NO,
                        2,
                        P_CREDITED_TAX_PERIOD_NO,
                        P_WDEE_TAX_ACCOUNT_NO,
                        P_TOTAL_BASE_SALARY,
                        P_ENTER_USER,
                        sysdate
                      );
                       --insert data to tax_transection table 
                        insert into TAX_TRANSACTION 

                       (    TAX_TRANS_NO,
                            IRD_EMPLOYEE_NO,
                            TAX_CENTRE_NO,
                              TRANS_DATE,
                            ENTER_DATE,
                            ENTER_TIME,
                            AMOUNT,
                            TAX_ACCOUNT_NO,
                              TAX_PAYER_NO,
                            TAX_TYPE_NO

                        )
                        Values 
                        (
                            V_TAX_TRANS_NO,
                            P_IRD_EMPLOYEE_NO,
                            P_TAX_CENTRE_NO,
                              sysdate,
                            sysdate,
                            SYSTIMESTAMP,
                            P_AMOUNT,
                            P_WDEE_TAX_ACCOUNT_NO,
                              P_TAX_PAYER_NO,
                            2);
                            --insert data to sub transetion table
                              insert into TAX_SUB_TRANS(
                              TAX_SUB_TRANS_NO,
                              CHARGE_TYPE_NO,
                              TAX_TRANS_TYPE_NO,
                              TAX_TRANS_NO, 
                              AMOUNT,
                              ENTER_DATE,
                              ENTER_TIME,
                              TAX_PERIOD_NO,
                              SUB_TRANS_COMMENT,
                              TAX_ACCOUNT_NO,
                              TAX_CENTRE_NO,
                              TAX_TYPE_NO,
                              CREDIT_SOURCE_TYPE_NO,
                              COLLECTION_LOCATION_NO,
                              ARREARS_FLAG,
                              REVERSE_FLAG,
                              APPEAL_DEPOSIT_FL
                        )
                        Values 
                        (
                          V_TAX_SUB_TRANS_NO,
                          15,
                          1,
                          V_TAX_TRANS_NO, 
                          P_AMOUNT,
                          sysdate,
                          SYSTIMESTAMP,
                          P_TAX_PERIOD_NO,
                          'Asp.net insert',
                          P_WDEE_TAX_ACCOUNT_NO,
                          P_TAX_CENTRE_NO,
                          2,
                          1,
                          2,
                          'N',
                          'N',
                          'N');

              update TAX_WITHHELD_DETAIL set TAX_TRANS_NO=V_TAX_TRANS_NO where TAX_WITHHELD_DETAIL_NO=V_TAX_WITHHELD_DETAIL_NO;
                          --if some exception occur in these four insertion database will be roolback

    END;

它提供异常由于对象的当前状态,操作无效 如果有人知道这个问题要解决或建议一个好的方法,使用oracle Transaction将多个记录插入到C#中的不同表中。非常感谢

0 个答案:

没有答案