Solved

Testing for DB Connectivity

Posted on 2011-09-20
5
228 Views
Last Modified: 2012-05-12
The app I'm writing is to be put on a laptop where it may or might not have connectivity to a SQL Server database (depending on whether the user is in the field or in a motel room).

Other than attempting to open a connection and waiting to see if it will time out, is there any other way to test to see if there is a database connection available?

TIA
0
Comment
Question by:Clif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Author Comment

by:Clif
ID: 36567865
Currently I have a function like this:
Public Function validConnection() As Boolean
    Dim bRetVal As Boolean = True
    Dim sqlConn As New SqlConnection() 'The SQL Connection

    sqlConn.ConnectionString = m_sConnectString 'Set the Connection String
    Try
        sqlConn.Open() 'Open the connection
    Catch
        bRetVal = False
    End Try
    sqlConn.Close()
    Return bRetVal
End Function

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36570284
First step is to see if the network is connected

Use My.Computer.Network.IsAvailable

http://msdn.microsoft.com/en-us/library/d1e9b5cx(v=vs.80).aspx

Then you can ping the server
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

Or you can just reduce the SQLConnection.ConnectionTimeOut and try to connect to the server. The default timeout is 15 seconds.
0
 
LVL 10

Author Closing Comment

by:Clif
ID: 36573517
SQLConnection.ConnectionTimeOut is readonly

However testing for the network and pinging the server is enough.

Thanks.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36576013
The connection timeout can be set using the connection string.
0
 
LVL 10

Author Comment

by:Clif
ID: 36576239
Oh, right.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question