Created
October 10, 2017 03:14
-
-
Save iwouldnot/a6e4e9376f164209bf5f6b49d90bbb03 to your computer and use it in GitHub Desktop.
singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class DBConnection | |
{ | |
private static SqlConnection instance = null; | |
private static readonly object padlock = new object(); | |
private DBConnection() | |
{ | |
} | |
public static SqlConnection Instance | |
{ | |
get | |
{ | |
lock (padlock) | |
{ | |
if (instance == null) | |
{ | |
instance = new SqlConnection(conStr); | |
} | |
return instance; | |
} | |
} | |
} | |
public static void Open() | |
{ | |
if (instance != null) | |
instance.Open(); | |
} | |
public static void Close() | |
{ | |
if (instance != null) | |
instance.Close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment