1

I want to make connection with sql server DB and maintain in singleton pattern. So is this functionality inbuilt in dot net ? or we mannually have to write the code for this scenario?

3
  • What are the reasons to use the singleton pattern? Are you worried that too many connection get opened at once? I am asking because maybe you don't need it at all. Commented Sep 2, 2010 at 9:49
  • possible duplicate of getting db connection through singleton class Commented Sep 2, 2010 at 9:55
  • The usual way is not to use a singleton, but to use connection pooling. The good thing here is that connection pooling is built in .NET and works out of the box. Commented Sep 2, 2010 at 9:57

3 Answers 3

1

A lazy loaded singleton example

public sealed class Singleton

{ Singleton() { }

public static Singleton Instance
{
    get
    {
        return Nested.instance;
    }
}

class Nested
{
    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Nested()
    {
    }

    internal static readonly Singleton instance = new Singleton();
}

}

1

Make use of sqlHelper class will do work for you which is related to database connections

1
  • @Lalit - sqlhelper contains all static methods so there is no need to bother about object creation it manage sqlconnection internally Commented Sep 3, 2010 at 5:46
1

See here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.