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,251
  • 4
  • 37
  • 52
Red Swan
  • 15,157
  • 43
  • 156
  • 238
  • 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
  • 175,020
  • 35
  • 237
  • 263
  • @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,362
  • 8
  • 58
  • 95
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,691
  • 5
  • 35
  • 55