2 Replies - 3351 Views - Last Post: 22 August 2012 - 07:18 PM Rate Topic: -----

#1 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Share or Create a New SqlConnection Between Static Methods?

Posted 22 August 2012 - 03:22 PM

Hello C# guys, I'm kinda stomped as to whether it's better to pass a SqlConnection object between static methods or initialize new connections within each one. For example:

The first class' some_method() does not accept the connection as a parameter while the second does.
class SomeClass
{
    public static void some_method()
    {
        using (SqlConnection connection = new SqlConnection("......."))
        {
            using (SqlCommand command = new SqlCommand("......."))
            {
                connection.Open();

                // ... code

                connection.Close();
            }
        }
    }
}

//accept the connection as a parameter
class SomeOtherClass
{
    public static void some_method(SqlConnection connection)
    {
        using (connection)
        {
            using (SqlCommand command = new SqlCommand("......"))
            {
                //no open and close
            }
        }
    }
}



Will the second cause problems in terms of opening a connection that is already and so forth? I read that the using statement will Dispose of the object it is using so will this cause another issue altogether?

Thanks.

This post has been edited by codeprada: 22 August 2012 - 03:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Share or Create a New SqlConnection Between Static Methods?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Share or Create a New SqlConnection Between Static Methods?

Posted 22 August 2012 - 06:55 PM

Instead of complicating the design with lots of classes all connecting to the database, why not have just one class that is the traffic cop for the application?

Have all your other forms talk to the traffic cop instance.

Later if you update the application to use a different database, web service etc. you won't break all those other forms, you won't have to scrub through and copy/paste all your query code because they are insulated from that. You only have to update once class one time.
Was This Post Helpful? 3
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,101
  • Joined: 18-April 07

Re: Share or Create a New SqlConnection Between Static Methods?

Posted 22 August 2012 - 07:18 PM

tlhIn`toq is giving you some good advice here. With such a design you are decoupling your code from the database, abstracting database connections into its own layer and if you make it generic enough it can be something that you store away in a library for use on other projects (code reuse). All very good things.

Your classes should virtually be unaware that it is even talking to a database. The only one that should know anything is that traffic cop class.

:^:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1