0

I would like to poll the status of my connection. To achieve this aim I have this block of code:

 AdomdConnection adomdConnection = new AdomdConnection("MYConnectionSTRING");
            adomdConnection.Open();
            while (true)
            {
                Console.WriteLine(adomdConnection.State.ToString()+ "  "+DateTime.Now.ToString());
                System.Threading.Thread.Sleep(3000);
            }

it returns me every 3 second the status of the connection. But after I shut down my service it ruturns me also open. Can someone explain me why`? and how can I figure out the status of my connection to analysis Service?

UPDATE My actual requirement is: I would like to have an eternally connection to my Analysis service. (To send it every 3 seconds a query). So I need a live connection to Analysis Service. My fair is, that the connection timed out somtime. And If the connection is timed out or other thing is happend that force my connection to close. I would like to establish the connection to my analysis Service again How can I do that?

Kaja
  • 2,962
  • 18
  • 63
  • 99
  • 2
    Why poll it? Just create a connection object when you need it and dispose with `using`. Let the driver worry about connection pooling and making sure you have a live connection. Note that most drivers will not actually ping the server when you read `connection.State`, they only update it when a command is sent – Charlieface Oct 01 '21 at 11:43
  • Does this answer your question? [Why does C# SqlConnection.StateChange event NOT get fired in real time when SQL Server database is not available?](https://stackoverflow.com/questions/61652100/why-does-c-sharp-sqlconnection-statechange-event-not-get-fired-in-real-time-when) – Charlieface Oct 01 '21 at 11:46
  • Thank you for your answer. I have updated my Post to explin what I need actually – Kaja Oct 01 '21 at 12:07
  • Just catch the timeout exception and reopen the connection, no need to poll anything. Again, the driver manages the connection pool, so you should really just dispose the connection object and reopen when needed. See [C# Data Connections Best Practice?](https://stackoverflow.com/questions/17552829/c-sharp-data-connections-best-practice) – Charlieface Oct 01 '21 at 12:15
  • Sorry but how can I catch the timeout exception?? – Kaja Oct 01 '21 at 12:25
  • With a `try/catch` like you normally do?? Surely you already catch exceptions that come back from the server when issuing a command? – Charlieface Oct 01 '21 at 12:26

0 Answers0