Use Class and create a public function for creating data connection
here is the code
public bool OpenConnection(MySqlConnection Conn)
{
String connStr = "your connection string"
if (Conn == null)
{
errorMessage = "Connection Object doesn't exists. Internal Error";
return false;
}
if (Conn.State != ConnectionState.Open)
{
try
{
Conn.ConnectionString = connStr;
Conn.Open();
return true;
}
catch (Exception objE)
{
}
}
else
{
return true;
}
}