I have the following code:
class Program
{
static void Main()
{
var connection = new SqlConnection("myConnectionString");
connection.Open();
connection.StateChange += HandleSqlConnectionDrop;
Console.WriteLine("Hi");
Console.ReadLine();
}
private static void HandleSqlConnectionDrop(object connection, StateChangeEventArgs args)
{
Console.WriteLine("DB change detected");
}
}
I start the above code while the SQL server instance is running. I then proceed to execute
SHUTDOWN WITH NOWAIT;
on the sql server instance that the program is connected to. I then observer the SQL server service stopping. However, I never see the "DB change detected" message in the output. Why is this?
Aside: I will see the StateChange handler get called if I then attempt to perform an operation on the SQL connection, but never before hand. Is there a way this behavior can be changed?
connection.Open()
after.StateChange+=...
to see that it works. – Pantelegraphselect 1
– Hesson