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

  • SEARCH
  • Home
  • 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 6948679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:51:16+00:00 2026-05-27T13:51:16+00:00

What is the best way to put transactionality around the following C# db read

  • 0

What is the best way to put transactionality around the following C# db read code accessing a MySQL db below. And maybe the more important question is “is there any need to ever put transactionality around single read statements”

    public List<Item> RunQuery(DateTime runDate)
    {
        var connection = new MySqlConnection(CONNECTION_STRING);
        connection.Open();
        string query = "select * from MyTable order by name";
        var cmd = new MySqlCommand(query, connection);
        MySqlDataReader myReader = cmd.ExecuteReader();
        var items = new List<Item>();
        try
        {
            while (myReader.Read())
            {
                items.Add(new Item
                {
                    Name = myReader.GetString("name"),
                    Date = myReader.GetDateTime("date")
                });
            }
        }
        finally
        {
            myReader.Close();
            connection.Close();
        }
        return items;
    }
  • 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-05-27T13:51:17+00:00Added an answer on May 27, 2026 at 1:51 pm

    As Nitin Midha mentioned, there is no need to use a transaction when you only read data, or even if your code updates data but with only one database access (i.e., calling a SP that does the insert, update and or delete for you).
    Having said that, Transactions should be as atomic as possible, so they should be created immmediately before the first database update access and immediately after the last one. If it’s inside a try catch block, you can define de transaction variable before the try, then open it when needed inside the try, then in the catch section ask if transaction != null and if so then rollback. The commit would be in the try block.

    For instance:

            DbTransaction tx = null;
            try
            {
                tx = Connection.CreateTransaction();
                Connection.ExecuteNonQuery(tx, "SPNameOrSQLInstruction", new object[] { param1, param2 });
                tx.Commit();
            }
            catch (Exception ex)
            {
                if(tx != null)
                    tx.Rollback();
                Logger.Log (ex);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to put javascript/html/css code in the maven repository, so
What is the best way to put a text box on the following HTML
I asked a question earlier about the best way to put edit/delete links inline
I have the following question: Let's assume the code below: ... <h:form id=..> <table>
What is the best way to put comments in your code? I see at
what is the best way to do it ? put it in iframe .
Any best way to run the jasmine HTML reporter with browserify styled code? I
The best way to store images into MySQL is by storing the image location
What is the best way to put together a shortcut to launch a IronRuby
I'm trying to figure out the best way to put complex numbers into my

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.