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 6019281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:23:55+00:00 2026-05-23T03:23:55+00:00

I have a long-running service with several threads calling the following method hundreds of

  • 0

I have a long-running service with several threads calling the following method hundreds of times per second:

void TheMethod()
{
    using (var c = new SqlConnection("..."))
    {
        c.Open();

        var ret1 = PrepareAndExecuteStatement1(c, args1);
        // some code
        var ret2 = PrepareAndExecuteStatement2(c, args2);
        // more code
    }
}

PrepareAndExecuteStatement is something like this:

void PrepareAndExecuteStatement*(SqlConnection c, args)
{
    var cmd = new SqlCommand("query", c);
    cmd.Parameters.Add("@param", type);
    cmd.Prepare();
    cmd.Parameters["@param"] = args;

    return cmd.execute().read().etc();
}

I want reuse the prepared statements, preparing once per connection and executing them until the connection breaks. I hope this will improve performance.

Can I use the built-in connection pool to achieve this? Ideally every time a new connection is made, all statements should be automatically prepared, and I need to have access to the SqlCommand objects of these statements.

  • 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-23T03:23:57+00:00Added an answer on May 23, 2026 at 3:23 am

    Suggest taking a slightly modified approach. Close your connection immedately after use. You can certainly re-use your SqlConnection.

    The work being done at //some code may take a long time. Are you interacting with other network resources, disk resources, or spending any amount of time with calculations? Could you ever, in the future, need to do so? Perhaps the intervals between executing statement are/could be so long that you’d want to reopen that connection. Regardless, the Connection should be opened late and closed early.

    using (var c = new SqlConnection("..."))
    {
        c.Open();
        PrepareAndExecuteStatement1(c, args);
        c.Close();
        // some code
        c.Open();
        PrepareAndExecuteStatement2(c, args);
        c.Close();
        // more code
    }
    

    Open Late, Close Early as MSDN Magazine by John Papa.

    Obviously we’ve now got a bunch of code duplication here. Consider refactoring your Prepare...() method to perform the opening and closing operations.

    Perhaps you’d consider something like this:

    using (var c = new SqlConnection("..."))
    {
        var cmd1 = PrepareAndCreateCommand(c, args);
    
        // some code
        var cmd2 = PrepareAndCreateCommand(c, args);
    
        c.Open();
        cmd1.ExecuteNonQuery();
        cmd2.ExecuteNonQuery();
        c.Close();
        // more code
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long running Service method (doing business logic) and I would like
I have several long running report type transactions that take 5-10 minutes. Would I
I have a long running insert transaction that inserts data into several related tables.
I have a long running server side process (gets data via a web service
I have an activity that starts a long-running service which in turn adds an
I have a Windows service which performs a fairly long running task. At the
I have a windows service which spawns off long-running tasks while polling for them.
I have a long running Task that uses callbacks to feed data incrementally (rather
I have a long running SQL statement that I want to run, and no
I have a long running process in VB6 that I want to finish before

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.