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?

Arseny
  • 7,141
  • 4
  • 36
  • 52
Red Swan
  • 14,573
  • 43
  • 152
  • 235
  • 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. – Theo Lenndorff Sep 02 '10 at 09:49
  • possible duplicate of [getting db connection through singleton class](http://stackoverflow.com/questions/814206/getting-db-connection-through-singleton-class) – Fredrik Mörk Sep 02 '10 at 09: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. – Albin Sunnanbo Sep 02 '10 at 09:57

3 Answers3

1

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

Pranay Rana
  • 170,430
  • 35
  • 234
  • 261
  • @Lalit - sqlhelper contains all static methods so there is no need to bother about object creation it manage sqlconnection internally – Pranay Rana Sep 03 '10 at 05:46
1

See here.

Community
  • 1
  • 1
rkellerm
  • 5,082
  • 8
  • 53
  • 91
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();
}

}

nkr1pt
  • 4,591
  • 4
  • 32
  • 55