Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:39:53+00:00 2026-06-02T09:39:53+00:00

I need to add transactions for a sql statement in a database application, up

  • 0

I need to add transactions for a sql statement in a database application, up to this point, I have not need to do this, as I was primarily pulling data, or doing very small changes to existing data.

I have been using a tidy construct quite successfully with nested using statements, but I want to check with people who know better than me if this modified construct will work as I expect with the transaction embedded.

        using (SqlCommand cmd = new SqlCommand())
        using (cmd.Connection = new SqlConnection()) {
            cmd.Connection.ConnectionString = "...";
            cmd.Connection.Open();
            using (SqlTransaction tran = cmd.Connection.BeginTransaction()) {
                // do the work (try catch wraps the statements)
                // commit transaction if no errors found or rollback
            }
            cmd.Connection.Close();
        }

Thank you in advance
Regards
Martin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:39:55+00:00Added an answer on June 2, 2026 at 9:39 am

    There is a built in class TransactionScope that is easier to use. Instantiate it in a using block and anything inside it will be part of the same transaction.

    using(var ts = new TransactionScope())
    using (SqlCommand cmd = new SqlCommand()) 
    using (cmd.Connection = new SqlConnection()) 
    { 
      cmd.Connection.ConnectionString = "...";
      cmd.Connection.Open();
    
      // Do the work
    
      ts.Complete();
    
      cmd.Connection.Close();
    }
    

    If any exceptions occurs, the transactionscope will automatically abort all operations when disposed. The transactionscope also affects any operations done in any function called, without the need to pass db connections around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add a new column to a MS SQL 2005 database with
I currently have a large SQL statement which i add the following line to
I need to add an invite users functionality to my social networking application so
I need to add a dependency on a specific version of GDIPlus. This is
I need to add and delete class on an element having multiple classes: This
Transactional Replication on SQL Server 2005 Enterprise x64 (SP3). I need to add check
Our app needs to add large amounts of text to SQL Server 2005 database
In my application I need to call a Stored Proc Asynchronously. For this I
I have an existing stored procedure in SQL Server that I need to call
I am having this big database on one MSSQL server that contains data indexed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.