i need ur help .I need to develop a function in Application WPF , that will be fired each 30 seconds, to ping to server and check if it's not connect to sql server, we will display Error message .Is there a way to simply "ping" from C# SQL Server?
Thanks in advance!
Below is my connection code if I'm missing anything .
/// Test that the server is connected
private bool IsServerConnected()
{
using (SqlConnection connection = ConnexionSGBD())
{
try
{
connection.Open();
Console.WriteLine("SQL Connection successful.");
return true;
}
catch (SqlException)
{
Console.WriteLine("SQL Connection failled.");
msgException();
return false;
}
}
}
//La fonction qui permet d'exécuter une requête SQL et stocker le résultat dans un datatable
public System.Data.DataTable GetTable()
{
SqlConnection connection = ConnexionSGBD();
string sqlrequete = "SELECT * FROM BD;";
System.Data.DataTable table = new DataTable();
if (IsServerConnected()==true)
{
connection.Open();
using (SqlCommand cmd = new SqlCommand(sqlrequete, connection))
{
table.Load(cmd.ExecuteReader());
}
return table;
}
else
{
msgException();
return null;
}
}