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

The Archive Base Latest Questions

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

Is the following defensive programming? What I mean is that if it loses the

  • 0

Is the following defensive programming?

What I mean is that if it loses the connection, or some problem occurs during run-time and then the user runsit again will the .NET framework have tidied up any open connections and objects that were created when it first ran?

I’ve heard mention of a “Singleton pattern” – is this something I should use in the static method CreateConnection?

class Program {
  static void Main(string[] args) {
     DataTable CasTable = fillSampleDataTable("SELECT top 100 * FROM x");
     //do other stuff
  }


  static SqlConnection CreateConnection() {
     SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["XXX"].ConnectionString);
     return conn;
  }
  static SqlDataAdapter CreateAdapter(string myCommand) {
    SqlDataAdapter myAdapt = new SqlDataAdapter(myCommand, CreateConnection());
    return myAdapt;
  }
  static DataTable fillSampleDataTable(string myCommand) {
      using (var adapt = CreateAdapter(myCommand)) {                   
            DataSet mySet = new DataSet();
            adapt.Fill(mySet, "SampleData");
            return mySet.Tables["SampleData"];
    }
  }
 }
  • 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-11T10:07:11+00:00Added an answer on June 11, 2026 at 10:07 am

    I would recommend you using the ADO.NET connection pool, a.k.a disposing the connections as soon as you have finished using them => wrap all IDisposable resources in using statements:

    class Program 
    {
        static void Main(string[] args) 
        {
            DataTable CasTable = fillSampleDataTable("SELECT top 100 * FROM x");
            //do other stuff
        }
    
        static DataTable fillSampleDataTable(string myCommand) 
        {
            var connectionString = ConfigurationManager.ConnectionStrings["XXX"].ConnectionString;
            using (var conn = new SqlConnection(connectionString))
            using (var cmd = conn.CreateCommand())
            using (var adapt = new SqlDataAdapter(cmd, conn)) 
            {
                conn.Open();
                cmd.CommandText = myCommand;
                DataSet mySet = new DataSet();
                adapt.Fill(mySet, "SampleData");
                return mySet.Tables["SampleData"];
            }
        }
    }
    

    But normally DataSets and DataTables are artifacts of the past. Today you are better off using strongly typed models.

    So define a model:

    public class MyModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    and then write a method that will return a list of those models:

    class Program 
    {
        static void Main(string[] args) 
        {
            var models = SelectTop100Models("SELECT top 100 * FROM x");
            //do other stuff
        }
    
        static IEnumerable<MyModel> SelectTop100Models() 
        {
            var connectionString = ConfigurationManager.ConnectionStrings["XXX"].ConnectionString;
            using (var conn = new SqlConnection(connectionString))
            using (var cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText = "SELECT top 100 * FROM x";
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        yield return new MyModel
                        {
                            Id = reader.GetInt32(reader.GetOrdinal("ID")),
                            Name = reader.GetString(reader.GetOrdinal("Name")),
                        };
                    }
                }
            }
        }
    }
    

    Alternatively you might consider using an ORM framework such as the ADO.NET Entity Framework as it will simplify you querying the relational database and working directly with your strongly typed models using LINQ queries.

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

Sidebar

Related Questions

I'm trying to do some defensive programming here to avoid objects changing state in
Following this JavaFX 2.0 tutorial I created a css file in eclipse that includes
Following my previous question (I assume that 64-bit compiler uses only SSE instructions for
I have just inherited some code and part of that code is to show
Following is some obviously-defective code for which I think the compiler should emit a
Is the following stored procedure code robust for multi-user application. It is working fine.
I've got the following code which makes a connection to a db > runs
I am parsing some URL links with the following.. Document jsDoc2 = null; try
Following are the features that my app is using: Screen layouts: SMALL NORMAL LARGE
Following this tutorial http://msdn.microsoft.com/en-us/vstudio/hh543922.aspx , I'm trying to use the ReplaceNode method that should

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.