Solved

Testing for DB Connectivity

Posted on 2011-09-20
5
224 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
Comment Utility
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
Comment Utility
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
Comment Utility
SQLConnection.ConnectionTimeOut is readonly

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

Thanks.
0
 
LVL 83

Expert Comment

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

Author Comment

by:Clif
Comment Utility
Oh, right.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now