Transaction

L

Leeor Chernov

Hi,
I am using transaction object with this code:
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;
// CallMehtod1( command); //- executing query 1 with
command.ExecuteNonQuery();
// CallMehtod 2(command);//- executing query 2 with
command.ExecuteNonQuery();
transaction.Commit();


My question is it possible to execute different queries with different
method calls , and after commit the transaction like in the sample, rather
than execute the queries in the same method within the transaction, will
this transaction work well ?

Leeor
 
P

Petar Repac

Leeor said:
Hi,
I am using transaction object with this code:
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;
// CallMehtod1( command); //- executing query 1 with
command.ExecuteNonQuery();
// CallMehtod 2(command);//- executing query 2 with
command.ExecuteNonQuery();
transaction.Commit();


My question is it possible to execute different queries with different
method calls , and after commit the transaction like in the sample, rather
than execute the queries in the same method within the transaction, will
this transaction work well ?

Leeor

when you start an transaction you can freely pass your transaction and
connection objects to various methods and execute commands on this
transaction.

I is important not to loose a reference to your transaction object and
have an open connection as little time as you can (open late, close asap).


Regards, Petar Repac
 
L

Leeor Chernov

Tnx Petar,
One more question , Can I initialize new command object each call that
points the transaction reference like this:

command.Transaction = transaction;

Or should I keep the same Command reference as the first execution within
the transaction???

Leeor Chernov
 
P

Petar Repac

yes you can.

Look at the sample from
Implementing Database Transactions with Microsoft .NET
http://msdn2.microsoft.com/en-us/library/ms954625.aspx#psent_topic7

the sample uses the same command object and changes CommandText but it
would work even if you use another command object.

from documentation for SqlCommand.Transaction Property:
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETFX30SDK4VS.1033/cpref10/html/P_System_Data_SqlClient_SqlCommand_Transaction.htm
Gets or sets the SqlTransaction within which the SqlCommand executes.
 
L

Leeor Chernov

Thank you very much!!!!
Petar Repac said:
yes you can.

Look at the sample from
Implementing Database Transactions with Microsoft .NET
http://msdn2.microsoft.com/en-us/library/ms954625.aspx#psent_topic7

the sample uses the same command object and changes CommandText but it
would work even if you use another command object.

from documentation for SqlCommand.Transaction Property:
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETFX30SDK4VS.1033/cpref10/html/P_Syste
m_Data_SqlClient_SqlCommand_Transaction.htm
Gets or sets the SqlTransaction within which the SqlCommand executes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top