Showing posts with label havent. Show all posts
Showing posts with label havent. Show all posts

Wednesday, March 28, 2012

TableAdapters and db transactions (begin, comit,roleback....)

Hello

Doing some webapp. in ASP.NET and using som of tutorials from this site

http://www.asp.net/learn/dataaccess/default.aspx?tabid=63 . I havent seen

anything aboat transactions. Is it TableAdapters that runs the show ?

Or if not, where to implement it, becouse whan you to insert data you use a GridView or DetailsView

point to some methode yuo allready made in TableAdapter and thats it. Or am I missing somthing?

Thanks

Its Connection object which handles transactions for you.

SqlTransaction tran = Conn.BeginTransaction();

If you are using DataAdaptor, get the command object,

SqlCommand cmdDelete = DataAdaptor.GetDeleteCommand();
SqlCommand cmdInsert = DataAdaptor.GetInsertCommand();
SqlCommand cmdUpdate = DataAdaptor.GetUpdateCommand();

cmdDelete.Transaction = tran;
cmdInsert.Transaction = tran;
cmdUpdate.Transaction = tran;

Assign this tranaction object to a SQLCommand, use

tran.rollback, to rollbak transactions

tran.commit, to commit transactions

Hope this answers your question.


I think I've got it!!! :-)

ClientTableAdapter clientAdapt =newClientTableAdapter();

SqlConnection myConnection =newSqlConnection("server=(local)\\SQLExpress;Integrated Security=SSPI;database=Novabaza");SqlCommand myCommand =newSqlCommand();SqlTransaction myTrans;// Open the connection.

myConnection.Open();

// Assign the connection property.

myCommand.Connection = myConnection;

// Begin the transaction.

myTrans = myConnection.BeginTransaction();

// Assign transaction object for a pending local transaction

myCommand.Transaction = myTrans;

try

{

// Create a Savepoint

clientAdapt.InsertClientdata(TextBoxName.Text, TextBoxSurname.Text,Convert.ToInt32(TextBoxTelnr.Text));

myTrans.Commit();

Response.Write(

" Records are written to the database!");

}

catch (Exception ex)

{

myTrans.Rollback();

Response.Write("Record is not written to the database!");throw ex;

}

finally

{

myConnection.Close();

}

InsertClientdata() is a query procedure I've made within (Client)tableadapter.

Now I'm goign to try something about using several adapters and creating some "SavePoints" for RollBack

Thanks a lot