Skip to content

Instantly share code, notes, and snippets.

@IsTheJack
Created March 6, 2016 19:34
Show Gist options
  • Save IsTheJack/5bfc3679bd0962b1bb9c to your computer and use it in GitHub Desktop.
Save IsTheJack/5bfc3679bd0962b1bb9c to your computer and use it in GitHub Desktop.
Singleton connection class with C# (ADO.NET)
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;
}
}
}
}
@Blandine11
Copy link

Hello,
While using this singleton class, how can we create an instance in order to send request to the database?

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment