How to establish DB connection using singleton pattern

  • Thread starter Thread starter Sai Pranav
  • Start date Start date
S

Sai Pranav

Guest
Hi All, I have done R&D on Single ton design and tried to establish the connection. I am able to define the class with requirements but couldn't know how to call the method . Please help me on this.

public class Database
{
private static Database _instance=null;
private static SqlConnection con = null;
static object _syncObject = new object();
private Database()
{
string conString = "Data Source=localhost;Initial Catalog=Test;Integrated Security=True";
try
{
con = new SqlConnection(conString);
con.Open();

}

catch (Exception e)
{
con.Close();
Console.Write(e.Message);
}
}

public static Database Instance
{
get
{
if (_instance == null)
{
lock (_syncObject)
{
if (_instance == null)
{
_instance = new Database();
}
}
}
return _instance;
}
}


}

Continue reading...
 
Back
Top