Created
March 6, 2016 19:34
-
-
Save IsTheJack/5bfc3679bd0962b1bb9c to your computer and use it in GitHub Desktop.
Singleton connection class with C# (ADO.NET)
This file contains 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
using System.Data.SqlClient; | |
namespace Project.Infra | |
{ | |
// Singleton Class | |
class Connection | |
{ | |
// Connection's configuration: | |
private static string connectionString = @"YOUR-CONNECTION-STRING"; | |
private static Connection singleton; | |
private static SqlConnection sqlConnection; | |
public SqlConnection SqlConnetionFactory | |
{ | |
get { return sqlConnection; } | |
} | |
private Connection() { } | |
public static Connection Singleton | |
{ | |
get | |
{ | |
if (singleton == null) | |
singleton = new Connection(); | |
sqlConnection = new SqlConnection(connectionString); | |
return singleton; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
While using this singleton class, how can we create an instance in order to send request to the database?
Regards