首页 文章

数据库连接关闭时在Classic ASP中使用自定义错误消息

提问于
浏览
0

我们有一个运行IIS 6和ASP classic的站点 . 我们连接到Oracle数据库以收集要在此网页上显示的信息 . 当数据库关闭时,最终用户会看到有关无法连接到数据库的典型asp错误消息 . 我想显示一条自定义错误消息,其中包含“此时数据库当前不可用”的内容 . 任何人都可以使用oracle连接字符串在ASP中提供语法来实现此目的吗?

谢谢 .

2 回答

  • 0

    这是我用的;

    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
    

    显然,调整连接字符串以适合您的特定Oracle环境和数据库凭据 .

  • 1

    在经典的asp中有类似try语法的尝试 . 接下来使用错误恢复 . 请在Try-Catch-End Try in VBScript阅读更多内容 . 然后,您可以在数据库连接初始化之前将错误状态重置为0 . 您甚至可以使用函数来收集所有错误消息,并在最后显示 .

    dim error_string 'initiate the variable
    
                function readAndSetErrors(msg) 'Custom error message function
    
                    if err <> 0 then
                        error_string = error_string & "
    " 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)

相关问题