Results 1 to 4 of 4

Thread: Best way to test ms sql connection.

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Best way to test ms sql connection.

    hi guys! I just want to know if there is a better way, than the code below, for testing the connection to my ms sql 2000 server where in i can get the specific error message and "translate" it to words that my "dumb user" can understand. Thanks in advance!

    Code:
            public static bool TestDBConnection(string ConnStr)
            {
                bool returnVal = false;
                SqlConnection sqlCon = new SqlConnection(ConnStr);
                try
                {             
                    sqlCon.Open();
                    returnVal = true;
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Connection Failed!\r\nMessage: " + ex.Message, "Test Connection",
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally
                {
                    sqlCon.Close();
                }
    
                return returnVal;            
            }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Best way to test ms sql connection.

    Your code is incorrect, the finally block will try to close an unopened connection (If an exception was thrown while opening the connection) which means another exception
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Best way to test ms sql connection.

    You should interrogate the SqlException to get specific information. It has a Errors collection whose items contain information about the specific errors that occurred on the server. It also has other properties that may be useful. You should read the MSDN documentation for the SqlException class if using it in your code, as you should for all classes you use.

    Also, you should make use of a 'using' block, in which case the 'finally' block is not required:
    C# Code:
    1. using (SqlConnection sqlCon = new SqlConnection(ConnStr))
    2. {
    3.     try
    4.     {
    5.  
    6.     }
    7.     catch
    8.     {
    9.  
    10.     }
    11. }
    The connection will be disposed, which inherently means closed, at the final brace. Always employ 'using' blocks for short-lived, disposable objects.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Best way to test ms sql connection.

    Thanks for the info guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width