Solved

Testing for DB Connectivity

Posted on 2011-09-20
5
227 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

792 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