Jump to content

How To Deal With Sql Connection Error In Asp Code


kwilliams
 Share

Recommended Posts

We have an application that pulls data from several servers, and one of the external servers that we don't have control of went down recently. When our code attempted to open the connection string to that server, it pulled our entire application down.So I need to know how to check that a connection is working first *before* attempting to open it, so that it won't bring everything else down with it. here is what the connection string looks like:

Dim strConn As StringstrConn = "Provider=SQLOLEDB; Data Source=SERVERNAME; Initial Catalog=DATABASENAME; User ID=USERID; Password=PASSWORD"

And here is the line of code that had the error:

objConn = Server.CreateObject("ADODB.Connection")		objConn.Open (strConn) '<<<----- ERROR OCCURRED HERE!!!

And and all help is appreciated.

Link to comment
Share on other sites

So I need to know how to check that a connection is working first *before* attempting to open it
Opening a connection is what you do to test it. You could open a different connection, like a socket connection, and see if it connects to the hostname. But the ADODB connection provider does some things automatically (like figures out which port to use, etc), so you would need to add that in yourself to the socket code if you want to go that route.Another solution would be to detect when the error happens and deal with it instead of letting it be a fatal error. Look into exception handling for how to handle errors when they happen.
Link to comment
Share on other sites

Opening a connection is what you do to test it. You could open a different connection, like a socket connection, and see if it connects to the hostname. But the ADODB connection provider does some things automatically (like figures out which port to use, etc), so you would need to add that in yourself to the socket code if you want to go that route.Another solution would be to detect when the error happens and deal with it instead of letting it be a fatal error. Look into exception handling for how to handle errors when they happen.
Thanks justsomeguy,I received a similar response somewhere else with this example:...objConn = Server.CreateObject("ADODB.Connection")objConn.ConnectionTimeout = 10 On Error Resume Next objConn.Open (strConn)On Error GoTo 0If objConn.State <> 1 Then ... unable to connect to that db ... ... so you get to figure out how to handle that ... ... note that objConn is now useless, so don't attempt to use it ... Else ... should be okay End If...I've added that code without any problems, so we'll see if it works on the next error. Thanks.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...