I have implemented as follows, a class applying singleton pattern to get a global single access to database. I intend to provide a thread-safe implementation.
using System.Data.SqlClient;
public sealed class Database
{
private static volatile SqlConnection instance;
private static object syncRoot = new object();
private const string connectionString = "Data Source=ServerName;" +
"Initial Catalog=DataBaseName;" +
"User id=UserName;" +
"Password=Secret;";
private Database() { }
public static SqlConnection Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new SqlConnection(connectionString);
}
}
return instance;
}
}
}