Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Check connection in C#



After making a connection with connection object,
it is important for a programmer to validate the connection whether it is working or not.
Here, ForgetCode suggests a simple code to check for.

Assume that the connection object con1 was created already.


public int checkConnection()
        {
            // Try block with Executable commands 
            try
            {
                //Opening Connection
                con1.Open();
                return 1;
            }

            // Catch block -For tracking unsuccessful cases
            catch
            {
                return 0;
            }

            // Closing connection in 'must be Executed block'
            finally
            {
                con1.Close();
            }

        }



If the connection is opened successfully, 1 will be returned and the connection will be immediately closed by the finally block.

..