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?
-
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 LenndorffCommented Sep 2, 2010 at 9:49
-
possible duplicate of getting db connection through singleton class– Fredrik MörkCommented 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.– Albin SunnanboCommented Sep 2, 2010 at 9:57
Add a comment
|
3 Answers
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();
}
}
Make use of sqlHelper
class will do work for you which is related to database connections
-
@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