You may declare a SqlConnection field in your class like that
static class MySQLConnect
{
private static SqlConnection _Connection;
public static SqlConnection Connection
{
get
{
if(_Connection == null)
{
string cs = @"server=127.0.0.1; user id=root; password=''; database=cluster";
_Connection = new MySqlConnection(cs);
}
if(_Connection.State == ConnectionState.Closed)
try
{
conn.Open();
}
catch(Exception ex)
{
//handle your exception here
}
return _Connection;
}
}
}
you can then access your SqlConnection in old your project using
SqlConnection myConnection = MySQLConnect.Connection;
It's a quick example that suffers from having the connection opened during all the time of your project while it would be better to only open and close it through methods like "Open" and "Close" in your class.
so I'd suggest create a MyConnect class Singleton and use a non static SqlConnection object so you'll be able to make that better.