Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public static void AddNew(string stu_name, string address, string Phone, datetime date)
        {
       String sql = "Select I_Student_ID.NEXTVAL from dual";
       Int newStudent_ID = 0;
  OracleDatabase db = (OracleDatabase)DatabaseFactory.CreateDatabase("PHTS");
    using (System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql))
          {
            try
            {
                newStudent_ID = Convert.ToInt32(db.ExecuteScalar(cmd));
            }
            catch (Exception e)
            {
              cmd.Transation.Rollback();
             }
     
        OracleDatabase db = (OracleDatabase)DatabaseFactory.CreateDatabase("PHTS");
 
 using (System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("AddNewStudent"))
            {
                db.AddInParameter(cmd, "I_Student_ID",DbType.Int32,newStudent_ID);
                db.AddInParameter(cmd, "I_STU_Name", DbType.String,stu_name);
                db.AddInParameter(cmd, "I_Address", DbType.String,address);
                db.AddInParameter(cmd, "I_Phone", DbType.String,Phone);
                db.AddInParameter(cmd, "I_Date", DbType.date,date);
 
               db.ExecuteNonQuery(cmd);
 
            }
 
        }
    }

I want to Rollback the sequence number for Student_Id if the transation has some problems. I am not sure how can I rollback the transation because the above code is not working, I can't rollback the .Nextval.

Thanks
Posted

1 solution

What I do:
C#
public static DataTable FillDataTable(String StoredProcedure, List<sqlparameter> sqlParameters, String ConnectionName)
        {
            DataTable tabel = new DataTable("GeneralTable");
            SqlTransaction transaction = null;
            using (SqlCommand cmd = BaseDatabaseData(StoredProcedure, sqlParameters, ConnectionName))
            {
                try
                {
                    using (transaction = cmd.Connection.BeginTransaction("SampleTransaction"))
                    {
                        cmd.Transaction = transaction;
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        adapter.Fill(tabel);
                        transaction.Commit();
                    }
                }
                catch (Exception ex)
                {
                    String Message = ex.Message;
                    transaction.Rollback();
                    throw new Exception(ex.Message, ex.InnerException);
                }
                finally
                {
                    if (cmd.Connection.State.Equals(ConnectionState.Open))
                    {
                        cmd.Connection.Close();
                        cmd.Connection.Dispose();
                    }
                }
            }
            return tabel;
        }
</sqlparameter>

I know I use the MSSql connection and not the Oracle one. I believe that setting up a transaction will work with the Oracle connection as with the mssql connection.
Method BaseDatabaseData returns me an open SqlConnection
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900