I have a winform program that uses Merge Replication to keep a local SQL Express sync’d with a Central SQL Server on our network. Due to the nature of this app the sync process runs automatically on App open and close. The way I am doing it (below) seems quite time consuming. What would be a faster way?
Thinking outside the box it would probably be okay if I just checked for the Server itself though that could present a hole for an edge case where the Server is available but SQL is down. Alternately, maybe just checking if our domain is available though that could leave the same hole I guess.
Thanks!
private static bool IsSqlAvailable()
{
try
{
var conn = new SqlConnection("Data Source=WWCSTAGE;Initial Catalog=Connect;Integrated Security=True");
conn.Open();
conn.Close();
HUD.IsSQLAvailable = true;
return true;
}
catch (Exception)
{
HUD.IsSQLAvailable = false;
return false;
}
}
As an additional note. The above is particularly slow when off network. I even set the Connection Timeout = 1 and it still sits at this point for quite some time.
My usual advice in this kind of scenario, where you’re checking for the existence/connectivity of something that is out of your control is: don’t.
Your check can pass and an instant later, the result is no longer true. So just try to do whatever it is you want to do, assuming that the SQL server is available, and handle the inevitable errors gracefully.