Data Access Application Block – Transactions

Topics: Data Access Application Block
Mar 5, 2010 at 3:29 PM

Hello

About The Enterprise Library Data Access Application Block – Transactions

I’m trying to accomplish the fallowing scenery

I’m trying to use transactions so if something goes wrong the first inserts (names) should never happen

The INSERT_LastNames stored procedure doesn´t exists so the bussines method will allways throw an exception and for that the RollbackTransaction methos in the dal class will be execute.

THIS IS NOT WORKING can you help me?

DAL Class:

public class DalAccesoDatos

    {

        static string EntLibDB = "";

        Database db = DatabaseFactory.CreateDatabase();

 

        DbConnection conexion;

        DbTransaction transaction;     

       

 

        public DalAccesoDatos()

        {

            EntLibDB = "ConectionString";

            db = DatabaseFactory.CreateDatabase(EntLibDB);            

        }

 

        public void BeginTransaction()

        {

            conexion = db.CreateConnection();                       

            conexion.Open();

            transaction = conexion.BeginTransaction();           

        }

 

        public void CommitTransaction()

        {

            transaction.Commit();

        }

 

        public void RollbackTransaction()

        {          

            transaction.Rollback();           

        }

 

        public object ExecuteScalar(string procedimiento, params object[] parametros)

        {

            DbCommand command = db.GetStoredProcCommand(procedimiento, parametros);

            return db.ExecuteScalar(command);

        }       

 

        public DataTable ExecuteDataTable(string procedimiento, params object[] parametros)

        {

            DbCommand command = db.GetStoredProcCommand(procedimiento, parametros);          

            return db.ExecuteDataSet(command).Tables[0];

        }       

 

        public int ExecuteNonQuery(string procedimiento, params object[] parametros)

        {

            DbCommand command = db.GetStoredProcCommand(procedimiento, parametros);

            return Convert.ToInt32(db.ExecuteNonQuery(command));

        }

       

    }

Bussines Class:

public class Pruebas

    {

        private int AddName( string name)

        {

            DalAccesoDatos AccesoDatos = new DalAccesoDatos();           

            return Convert.ToInt32(AccesoDatos.ExecuteScalar("INSERT_Names",  name));

        }                

 

        //Prueba Transacciones

 

        public void AddFullName (string name, string lastName)

        {

            DalAccesoDatos AccesoDatos = new DalAccesoDatos();

            AccesoDatos.BeginTransaction();

            try

            {

                int idName = AddName (name);

                AccesoDatos.ExecuteNonQuery("INSERT_LastNames", idName, lastName);

                AccesoDatos.CommitTransaction();

            }

            catch

            {

                AccesoDatos.RollbackTransaction();

                throw;

            }           

        }

    }

 

My data base has two tables Name and LastName

Name

 

 

PK-Identity

id

int

 

name

varchar(50)

     

LasName

 

 

PK-Identity

id

int

FK

idName

int

 

LasName

varchar(50)

 

Mar 8, 2010 at 1:42 AM

You should use the overload of ExecuteNonQuery/ExecuteScalar which passes an instance of DbTransaction.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 8, 2010 at 4:15 PM

Thanks, that was what i was missing