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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:54:48+00:00 2026-06-10T10:54:48+00:00

I have a command-line utility written in C#, which updates some records in the

  • 0

I have a command-line utility written in C#, which updates some records in the database and then enters a loop checking some other stuff. For some reason, the SQL execution always leaves a sleeping awaiting command session in SQL server 2008 after SQL execution. I can use SQL server activity monitor and run sp_who2 to confirm that. But SQL transaction has been committed successfully indeed. I can tell this from debugging in the code and the timestamp of the data record in the database.
Also, the open sleeping session will be killed when I explicitly stop the command-line utility. This indicates that my function creates some SQL connection object that can’t be disposed of until it is garbage collected by CLR when the application is aborted. How could this happen? Thanks. Here is the code:

bool result = false;
using (SqlConnection conn = new SqlConnection(this.Connection))
{

    using (SqlCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText =
                @"if not exists (select id from pl where sj=@sj and ej=@ej and dateInactivated is null) 
                    insert into pl(sj,ej,pf, br, tk, lastModified )
                    values(@sj,@ej,@pf,@br,@tk,getDate())
                else
                    update pl set sj=@sj,ej=@ej,pf=@pf,br=@br,tk=@tk,lastModified=getDate()
                    where sj=@sj and ej=@ej and dateInactivated is null
                ";

        cmd.Parameters.AddWithValue("@sj", sj);
        cmd.Parameters.AddWithValue("@ej", ej);
        cmd.Parameters.AddWithValue("@pf", pf);
        cmd.Parameters.AddWithValue("@br", br);
        cmd.Parameters.AddWithValue("@tk", tkData);
        cmd.CommandTimeout = 60;
        SqlTransaction trans = null;
        try
        {
            conn.Open();
            trans = conn.BeginTransaction();
            cmd.Transaction = trans;
            cmd.ExecuteNonQuery();
            trans.Commit();
            conn.Close();
            result = true;
        }
        catch (SqlException ex)
        {
            if (trans != null)
            {
                trans.Rollback();
            }
            Log.WriteLog(LogLevel.ERROR, ex.Message);
            result = false;
        }
        finally
        {
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
        }
    }

}

return result;

Note that tk field is an xml database field.

  • 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-10T10:54:49+00:00Added an answer on June 10, 2026 at 10:54 am

    This is called connection pooling, and you want it to work this way.

    Basically, dropping a connection entirely and then re-establishing it for your app incurs a much higher penalty than just re-using an idle connection that was left behind from a previous invocation of your code. This is a good thing.

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

Sidebar

Related Questions

I have a program which runs an external, command line utility and reads the
I have command line utility written in ANSI C on a Mac with a
I have a tiny command line utility which reads user input and adds all
I have a command line utility from a third party (it's big and written
I have a command line tool written in c# (that i have the source
I have a command line that I'm trying to modify to remove some of
I have some command-line ruby scripts for things like pre-processing text files before uploading
I wrote a command line utility using Zend Framework to do some nightly reporting.
I want to make a command-line utility that does some operations based on reflection
I've written a command line utility that uses getopt for parsing arguments given on

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.