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

Re: Transaction by Petar

Petar
Wed Jan 24 03:30:59 CST 2007

Leeor Chernov wrote:
> 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

Re: Transaction by Leeor

Leeor
Wed Jan 24 03:45:36 CST 2007

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


> >
> >
>
> 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



Re: Transaction by Petar

Petar
Wed Jan 24 04:02:46 CST 2007

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.



Leeor Chernov wrote:
> 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
>
>
>>>
>> 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
>
>

Re: Transaction by Leeor

Leeor
Wed Jan 24 10:45:11 CST 2007

Thank you very much!!!!
"Petar Repac" <petar.repac@replace_with_google_mail_service.com> ???
??????:#XsjN75PHHA.3316@TK2MSFTNGP05.phx.gbl...
> 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.
>
>
>
> Leeor Chernov wrote:
> > 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
> >
> >
> >>>
> >> 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
> >
> >