ADO 執行階段錯誤ADO Run-Time Errors

當您的程式發生執行階段錯誤時,會將 ADO 錯誤回報給您的程式。ADO errors are reported to your program as run-time errors. 您可以使用程式設計語言的錯誤捕捉機制來將它們設為陷阱和處理。You can use the error-trapping mechanism of your programming language to trap and handle them. 例如,在 Visual Basic 中,請使用 On Error 語句。For example, in Visual Basic, use the On Error statement. 在 Visual C++ 中,這取決於您用來存取 ADO 程式庫的方法。In Visual C++, it depends on the method you are using to access the ADO libraries. 使用 #import,請使用 try-catch 區塊。With #import, use a try-catch block. 否則,c + + 程式設計人員必須藉由呼叫 GetErrorInfo,明確地取出 error 物件。Otherwise, C++ programmers need to explicitly retrieve the error object by calling GetErrorInfo. 下列 Visual Basic sub 程式示範如何捕捉 ADO 錯誤:The following Visual Basic sub procedure demonstrates trapping an ADO error:

' BeginErrorHandlingVB01
Private Sub Form_Load()
' Turn on error handling
On Error GoTo FormLoadError

'Open the database and the recordset for processing.
Dim strCnn As String
strCnn = "Provider=sqloledb;" & _
    "Data Source=a-iresmi2000;" & _
    "Initial Catalog=Northwind;Integrated Security=SSPI"

' cnn is a Public Connection Object because
' it was defined WithEvents
Set cnn = New ADODB.Connection
cnn.Open strCnn

' The next line of code intentionally causes
' an error by trying to open a connection
' that has already been opened.
cnn.Open strCnn

' rst is a Public Recordset because it
' was defined WithEvents
Set rst = New ADODB.Recordset
rst.Open "Customers", cnn

Exit Sub

' Error handler
    Dim strErr As String
    Select Case Err
        Case adErrObjectOpen
            strErr = "Error #" & Err.Number & ": " & Err.Description & vbCrLf
            strErr = strErr & "Error reported by: " & Err.Source & vbCrLf
            strErr = strErr & "Help File: " & Err.HelpFile & vbCrLf
            strErr = strErr & "Topic ID: " & Err.HelpContext
            MsgBox strErr
            Debug.Print strErr
            Resume Next
        ' If some other error occurs that
        ' has nothing to do with ADO, show
        ' the number and description and exit.
        Case Else
            strErr = "Error #" & Err.Number & ": " & Err.Description & vbCrLf
            MsgBox strErr
            Debug.Print strErr
            Unload Me
    End Select
End Sub
' EndErrorHandlingVB01

Form_Load 事件程式會嘗試開啟相同的 連接 物件兩次,以刻意建立錯誤。This Form_Load event procedure intentionally creates an error by trying to open the same Connection object twice. 第二次呼叫 Open 方法時,就會啟動錯誤處理常式。The second time the Open method is called, the error handler is activated. 在此情況下,錯誤的類型為 adErrObjectOpen,因此錯誤處理常式會在繼續執行程式之前顯示下列訊息:In this case the error is of type adErrObjectOpen, so the error handler displays the following message before resuming program execution:

Error #3705: Operation is not allowed when the object is open.
Error reported by: ADODB.Connection
Help File: E:\WINNT\HELP\ADO260.CHM Topic ID: 1003705

此錯誤訊息包含 Visual Basic Err 物件所提供的每一項資訊,但 LastDLLError 值除外,此值不適用於此處。The error message includes each piece of information provided by the Visual Basic Err object except for the LastDLLError value, which does not apply here. 錯誤號碼會指出發生了哪些錯誤。The error number tells you which error has occurred. 當您不想要自行處理錯誤時,描述會很有用。The description is useful in cases in which you do not want to handle the error yourself. 您可以直接將它傳遞給使用者。You can simply pass it along to the user. 雖然您通常會想要使用針對應用程式自訂的訊息,但您無法預期每個錯誤;描述會針對發生錯誤的原因提供一些線索。Although you will usually want to use messages customized for your application, you cannot anticipate every error; the description gives some clue as to what went wrong. 在範例程式碼中, 連接 物件報告了錯誤。In the sample code, the error was reported by the Connection object. 您會在這裡看到物件的類型或程式設計識別碼-不是變數名稱。You will see the object's type or programmatic ID here - not a variable name.


Visual Basic Err 物件只包含最新錯誤的相關資訊。The Visual Basic Err object only contains information about the most recent error. 連接物件的 ADO Errors集合針對最近的 ADO 作業所引發的每個錯誤,各包含一個錯誤物件。The ADO Errors collection of the Connection object contains one Error object for each error raised by the most recent ADO operation. 使用 Errors 集合而非 Err 物件來處理多個錯誤。Use the Errors collection rather than the Err object to handle multiple errors. 如需有關 錯誤 集合的詳細資訊,請參閱 提供者錯誤For more information about the Errors collection, see Provider Errors. 但是,如果沒有有效的 連接 物件,則 ERR 物件是 ADO 錯誤相關資訊的唯一來源。However, if there is no valid Connection object, the Err object is the only source for information about ADO errors.

哪些種類的作業可能會導致 ADO 錯誤?What kinds of operations are likely to cause ADO errors? 常見的 ADO 錯誤可能牽涉到開啟物件(例如 連接記錄集)、嘗試更新資料,或呼叫您的提供者不支援的方法或屬性。Common ADO errors can involve opening an object such as a Connection or Recordset, attempting to update data, or calling a method or property that is not supported by your provider.

您也可以將 OLE DB 錯誤傳遞至您的應用程式,做為 錯誤 集合中的執行階段錯誤。OLE DB errors can also be passed to your application as run-time errors in the Errors collection.

下列主題提供有關 ADO 錯誤的詳細資訊。The following topic provides more information about ADO errors.