Test database connection

What are some good examples to test my database connection to a SQL database (SQL 2005 server)? I have some apps that run 24/7 on terminals and every now and then the network my come down with the apps still running. I'd like to write something in which they apps wouldn't have to restart once the network and database access comes back online.
LVL 5
thenrichAsked:
Who is Participating?
 
edemcsConnect With a Mentor Commented:
You could catch the SQL Exception and parse the message for connectivity.  Then you could just handle it gracefully until it tries again and the database is up and running.
0
 
thenrichAuthor Commented:
Yea right now that's my current line of thinking as well. I was hoping the frame work had something a bit more elegant then that.
0
 
edemcsCommented:
Not that I know of.  I've been using .NET for a long time and haven't seen anything specific to this scenario.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
edemcsCommented:
Just make sure you always close that connection in the Finally section of the Try/Catch
0
 
thenrichAuthor Commented:
Yup - i've been on .NET since beta. Was hoping maybe I missed something.
0
 
CodeCruiserCommented:
I use following in my project.



Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
            If My.Computer.Network.IsAvailable Then
                Try
                    If My.Computer.Network.Ping("dbase server") Then
                        frmMain.Enabled = True
                        MessageBox.Show("The connection to network has been restored.", "Network Reconnected", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Else
                        frmMain.Enabled = False
                    End If
                Catch
                End Try
            Else
                frmMain.Enabled = False
                MessageBox.Show("It looks like you have lost the network connection to the network.", "Network Disconnected", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.