Microsoft OLE DB Provider for ODBC Drivers error 80004005 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified /test.asp, line 60
<%@ LANGUAGE="VBs cript" %>
<%Response.Buffer = True
"設定buffer為True
On Error Resume Next
"開始錯誤處理
%>
<%"錯誤處理
If Err.Number <> 0 Then
"清除頁面
Response.Clear
"顯示錯誤資訊給使用者
%>
<HTML>
<HEAD>
<T
ITLE></TITLE>
</HEAD>
<BODY BGCOLOR="#C0C0C0">
<FONT FACE="ARIAL">An error occurred in the execution of this ASP page<BR>
Please report the following information to the support desk
<P><B>Page Error Object</B><BR>
錯誤 Number: <%= Err.Number %><BR>
錯誤資訊: <%= Err.Des cription %><BR>
出錯檔案: <%= Err.Source %><BR>
出錯行: <%= Err.Line %><BR>
</FONT>
</BODY>
</HTML>
<%End If%>
你們上面看到了,我首先設定On Error Resume Next ,這樣出現錯誤就不會影響程式的執行。
錯誤處理和資料庫
在錯誤處理中加入資料庫的執行是很複雜的。假若我們有一個程式,有很多的命令去向資料庫中添加記錄,如果insert/update在程式的最底部執行,如果我們前面又錯誤發生,那就完了!我們就會向資料庫中添加了一個錯誤的資訊。因為我們用了On Error Resume Next 一切的錯誤都被忽略了!即使前面出錯,程式依舊會向資料庫中添加資料的。
為避免這種情況,我們就先得做些手腳,正確處理的方法如下:
If Err.Number = 0 And objConnection.Errors.Count = 0 Then
"這裡才能執行語句,因為沒有錯誤
Set rstResults = dbData.Execute(txtSql)
End If
<% If Err.Number <> 0 Then
Response.Clear
Select Case Err.Number
Case 8
"指定錯誤的Number
"在這裡處理自訂錯誤
Case Else
"一般錯誤
If IsObject(objConnection) Then
If objConnection.Errors.Count > 0 Then
%>
<B>Database Connection Object</B>
<%
For intLoop = 0 To objConnection.Errors.Count - 1 %>
Error No: <%= objConnection.Errors(intLoop).Number %><br>
Des cription: <%= objConnection.Errors(intLoop).Des cription %><BR>
Source: <%= objConnection.Errors(intLoop).Source %><BR>
SQLState: <%= objConnection.Errors(intLoop).SQLState %><BR>
NativeError: <%= objConnection.Errors(intLoop).NativeError %><P>
<% Next
End If
End If
If Err.Number <> 0 Then %> <B>
Page Error Object</B><BR>
Error Number <%= Err.Number %><BR>
Error Des cription <%= Err.Des cription %><BR>
Source <%= Err.Source %><BR>
LineNumber <%= Err.Line %><P>
<%End If
End Select
End If %>
上面的例子讓我們一下了處理了很多在資料庫中出現的問題,這個在我們日常
編程也是常用的!我們也應該看到那個Select Case 語句,它能讓我們來處理特定的錯誤。
Redirect 和錯誤處理