问题
We have a site that is running IIS 6 and ASP classic. We connect to an Oracle database to gather information to display on this web-page. When the database is down the end-users see a typical asp error message about being unable to connect to the database. I would like to display a custom error message that would read something like "The database is currently unavailable at this time". Can anyone provide the syntax in ASP using an oracle connection string to accomplish this?
Thanks.
回答1:
This is what I use;
On Error Resume Next
Set con = Server.CreateObject( "ADODB.Connection" )
con.Open "Provider=myOracleProvider;Data Source=myOracleDB;User Id=myUsername;Password=myPassword;"
If Err.Number = 0 And con.Errors.Count = 0 Then
'No problems connecting to DB so don't do anything
ELSE
'Problems connecting to DB so do something to handle this or alert adm
END IF
On Error goto 0
Obviously adapt the connection string to suit your specific Oracle environment and database credentials.
回答2:
There is a try catch like syntax in classic asp. use an on error resume next. Please read more aout this at Try-Catch-End Try in VBScript. Then you can reset the error status to 0 just before the db connection initialization. You can even have a function to collect all the error messages and dispaly at the end.
dim error_string 'initiate the variable
function readAndSetErrors(msg) 'Custom error message function
if err <> 0 then
error_string = error_string & "<br/>" msg 'Add the custom error message if there is an error
end if
set err = 0 'rest the error
end function
On Error Resume Next
'Connection string
strConnection = "driver={oracle in instantclient_11_2};DataSource=XYZ;Uid=username;Pwd=password;"
Set objConn = Server.CreateObject("ADODB.Connection") ' create connection object
set err = 0 'Reset the err just in case
objConn.Open (strConnection ) ' open the connection
function readAndSetErrors("The database is currently unavailable at this time") 'execute the function
' more code and may be more function calls
Response.write(error_string)
来源:https://stackoverflow.com/questions/19799181/using-custom-error-message-in-classic-asp-when-database-connection-is-down