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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:38:39+00:00 2026-05-13T17:38:39+00:00

This thread is a continuation of Is there a reason to check for null

  • 0

This thread is a continuation of Is there a reason to check for null inside multiple using clausule in c#?

I’ve noticed that resharper lets me define using without opening any opening/closing bracket like in the method below (but then i can’t use defined vars later on if the brackets ain’t there, other then the one that is used exactly beneath defined using):

public static string sqlGetDatabaseRows() {
        string varRows = "";
        const string preparedCommand = @"
                    SELECT SUM(row_count) AS 'Rows'
                    FROM sys.dm_db_partition_stats
                    WHERE index_id IN (0,1)
                    AND OBJECTPROPERTY([object_id], 'IsMsShipped') = 0;";
        using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetailsDZP))
        using (var sqlQuery = new SqlCommand(preparedCommand, varConnection))
        using (var sqlQueryResult = sqlQuery.ExecuteReader())
            if (sqlQueryResult != null) {
                while (sqlQueryResult.Read()) {
                    varRows = sqlQueryResult["Rows"].ToString();
                }
                sqlQueryResult.Close();
            }
        return varRows;
    }

Is it good? Or should i use it like this?

public static string sqlGetDatabaseRows() {
        string varRows = "";
        const string preparedCommand = @"
                    SELECT SUM(row_count) AS 'Rows'
                    FROM sys.dm_db_partition_stats
                    WHERE index_id IN (0,1)
                    AND OBJECTPROPERTY([object_id], 'IsMsShipped') = 0;";
        using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetailsDZP)) {
            using (var sqlQuery = new SqlCommand(preparedCommand, varConnection))
            using (var sqlQueryResult = sqlQuery.ExecuteReader())
                if (sqlQueryResult != null) {
                    while (sqlQueryResult.Read()) {
                        varRows = sqlQueryResult["Rows"].ToString();
                    }
                    sqlQueryResult.Close();
                }
            varConnection.Close();
        }
        return varRows;
    }

sqlConnectOneTime looks like this:

    public static SqlConnection sqlConnectOneTime(string varSqlConnectionDetails) {
        SqlConnection sqlConnection = new SqlConnection(varSqlConnectionDetails);
        sqlConnect(sqlConnection);
        if (sqlConnection.State == ConnectionState.Open) {
            return sqlConnection;
        }
        return null;
    }

My questions are:

  1. Should I be closing varConnection by using varConnection.Close() and sqlQueryResult.Close(); in the end? (but this enforces use of brackets) or will the connection close itself when using is done.

  2. Should i be checking for NULL for varConnection since it’s possible it will return null (on the other hand resharper doesn’t complain here).

  3. Is there some better approach i could use for defining sqlConnectOneTime ? Like when the connection fails to open it should return ConnectionState.Closed instead of null?

Also just on a note i open up new connection every time i execute new query / update / insert since i am using threading and it was the smartest idea i could come up at that very moment. Feel free to suggest better one 🙂

I am asking about all this since i want to better understand whole process and stop making silly mistakes so be gentle with me.

MadBoy

Edit: modified question if varConnection.Close() and sqlQueryResult.Close() are nessecary if using is used.

  • 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-13T17:38:40+00:00Added an answer on May 13, 2026 at 5:38 pm
    1. Close() is called by the Dispose() method, so as long as you are using “using” properly you don’t need to call Close() explicitly.
    2. If it’s possible for a NULL to be returned, you should check for it. I would advise that if you control the code that gets the sql connection, you strongly consider throwing an exception rather than returning NULL. Otherwise other developers may run into the same sorts of problems. Failure to open a SQL connection that is required seems to me to be a valid exceptional case. If necessary, you can always include a TryConnectOneTime for when a developer wants to avoid exception handling.

    Also, another style note – I would advise properly bracing your using statements in case a stray extra line is added and an unexpected error occurs. Style-wise, I usually don’t tend to indent using statements when I have multiple statements together, but that’s all down to personal preference.

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

Sidebar

Related Questions

I was just reading this thread and it occurred to me that there is
In this thread some one commented that the following code should only be used
I recently read this thread on MSDN . So I was thinking of using
After reading many of the replies to this thread , I see that many
I got a comment to my answer on this thread: Malloc inside a function
In this thread, we look at examples of good uses of goto in C
I was browsing this thread , which has good recommendation but a bit too
I was reading this thread/post: https://stackoverflow.com/questions/262298/windows-c-ui-technology and am also wondering about a non .NET
It seems like Groovy was forgotten in this thread so I'll just ask the
Application has an auxiliary thread. This thread is not meant to run all the

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.