Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

SqlTransaction issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Piotr Strycharz - 15 Dec 2004 15:22 GMT
Hi,

The question is about how to check if the transaction is still active. Let's
say, that I am using a 3rd party stored procedure, that cannot be changed.
Sample is (Of course, the real one is much more complicated):
CREATE PROC test AS IF @@trancount > 0 ROLBACK TRAN

Now, I'm executing code:

tran = connection.BeginTransaction();
cmd = new SqlCommand("test", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();

After that - how do I know if the transaction is still active?

Regards
Sahil Malik - 15 Dec 2004 16:01 GMT
For this same purpose, The Microsoft Data Access block checks to see if the
transaction.connection is a valid connection or null.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik

> Hi,
>
[quoted text clipped - 13 lines]
>
> Regards
David Browne - 15 Dec 2004 16:30 GMT
> Hi,
>
[quoted text clipped - 10 lines]
> cmd.CommandType = CommandType.StoredProcedure;
> cmd.ExecuteNonQuery();

You can examine the @@trancount.  If it is > 0 then the transaction is
active.  Otherwise the transaction has been rolled back.

 new SqlCommand("select @tc = @@trancount)

and bind an output parameter to @tc.

David
bruce barker - 15 Dec 2004 16:59 GMT
| Hi,
|
[quoted text clipped - 13 lines]
|
| Regards
bruce barker - 15 Dec 2004 17:11 GMT
the commit will fail if no pending transaction exits - this will be your
fallback.

procs generally test @@trancount to avoid throwing an error on rollback when
there is no nested transction. you need to look to see how the proc is
returning failure - return value, raiserror, or paramter.

you are going to use multiple batches be sure to use the same connection,
and don't close it. at anytime you can select @@trancount to see a
transaction is active. you could also use the follow sp to do the commit.

   create proc DoCommit as
   set nocount on
   if @@trancount > 0
   begin
       commit tran
       select 1 as CommitResult
   end
   return 0 as CommitResult

then use

   cmd = new SqlCommand("DoComitt", connection);
   cmd.CommandType = CommandType.StoredProcedure;
   bool  ok = ((int) cmd.ExecuteScaler()) ==1);

-- bruce (sqlwork.com)

| Hi,
|
[quoted text clipped - 13 lines]
|
| Regards
Piotr Strycharz - 16 Dec 2004 08:28 GMT
> the commit will fail if no pending transaction exits - this will be your
> fallback.

Yes ! And it fails, indeed. However, my code is not aware of the ended
transaction, so it executes consecutive commands **without** transaction
(although transaction variable is not null).

Seems, that the @@trancount sql variable is the one solution.

Regards.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.