简体   繁体   中英

c# tableAdapter transaction rollback

I'm trying to commit and rollback transactions using a table adapter. I have the following code:

    try {
          MyTableAdapters.MyableAdapter contTa = new MyTableAdapters.MyableAdapter();    
          contTa.Connection.BeginTransaction();    
          contTa.InsertSP(myvalues);    
          contTa.Transaction.Commit();
         }
  catch {
          contTa.Transaction.Rollback();
         }

however it shows an error that the object is not initialized, I'm not sure what I'm doing wrong.

The insert command is a Stored Procedure that I must use in order to get the identity of the newly inserted values.

Using the debugger the error shows on contTa.Connection.BeginTransaction();

Try to declare the contTa variable outside try block:

MyTableAdapters.MyableAdapter contTa;
    try
        {
          contTa = new MyTableAdapters.MyableAdapter();

          contTa.Connection.BeginTransaction();

          contTa.Insert(myvalues);

          contTa.Transaction.Commit();
        }
        catch
        {
           contTa.Transaction.Rollback();
        }

After several attempts I found the solution, and I can have several functions linked with the same transaction, if it's useful for someone.

Here is my code:

public static long saveHeader(ref System.Data.SqlClient.SqlConnection RefConn, Client prst_h)
        {
            long? refInt = 0;
            HRM_TableAdapters.Client_HeaderTableAdapter contTa = new HRM_PrestamosTableAdapters.Client_HTableAdapter();
            contTa.Connection = RefConn;
            if (contTa.Connection.State == ConnectionState.Closed)
            {
                contTa.Connection.Open();
            }
            System.Data.SqlClient.SqlTransaction trans = contTa.Connection.BeginTransaction("SampleTrans");
            contTa.Transaction = trans;
            try
            {
                refInt = Convert.ToInt32(contTa.InsertClient_H(prst_h.values));
                prst_h.DocEntryPRST = (int)refInt.Value;
                calculateFixedCapFee(ref prst_h);
                if (prst_h.DetailList.Count > 0)
                {
                    if (dalPRST_AMORTIZACION.saveDetailList(ref RefConn, prst_h.DetailList, ref trans) != -1)
                    {
                        trans.Commit();
                    }
                    else
                    {
                        trans.Rollback();
                        refInt = -1;
                    }
                }
            }
            catch (Exception ex)
            {
                trans.Rollback();
                string error = ex.Message;
                refInt = -1;
            }
            finally
            {
                if (contTa.Connection.State != ConnectionState.Closed)
                {
                    contTa.Connection.Close();
                }
            }
            return refInt.Value;
        }

public static long saveClientList(ref System.Data.SqlClient.SqlConnection RefConn, LinkedList<DetailList> prst_DetailList, ref System.Data.SqlClient.SqlTransaction trans)
        {
            long? refInt = 0;
            HRM_TableAdapters.PRST_DetailTableAdapter contTa = new HRM_TableAdapters.PRST_DetailTableAdapter();
            contTa.Connection = RefConn;
            contTa.Transaction = trans;
            try
            {
                foreach (PRST_Detail prst in prst_DetailList)
                {
                    refInt = contTa.Insert(prst.Values);
                }
            }
            catch (System.Exception ex)
            {
                string error = ex.Message;
                refInt = -1;
            }
            return refInt.Value;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM