tailieunhanh - Microsoft SQL Server 2005 Developer’s Guide- P17
Microsoft SQL Server 2005 Developer’s Guide- P17:This book is the successor to the SQL Server 2000 Developer’s Guide, which was extremely successful thanks to all of the supportive SQL Server developers who bought that edition of the book. Our first thanks go to all of the people who encouraged us to write another book about Microsoft’s incredible new relational database server: SQL Server 2005. | Chapter 8 Developing Database Applications with ADO 319 error handler is fired enabling you to trap and respond to run-time errors. This tight integration with Visual Basic makes it easy to handle ADO errors. The following ShowError subroutine illustrates how ADO s error handling can be integrated with Visual Basic s On Error function Private Sub ShowError cn As Dim rs As New On Error GoTo ErrorHandler Select From no_such_table cn Exit Sub ErrorHandler DisplayADOError cn End Sub Here the ShowError function attempts to open a Recordset object against a nonexistent table. At the beginning of this function the On Error statement enables Visual Basic s error handler. In this case the On Error statement causes the program to branch to the ErrorHandler label when a trappable error is encountered. Executing the Open method with a nonexisting table causes the ADO object framework to generate a run-time error which in turn causes the program execution to resume with the first statement following the label. In this example the DisplayADOError subroutine is executed following the invalid Open attempt. The following code listing shows how the DisplayDAOError subroutine uses DAO s Error object and Errors collection to display information about an ADO error condition in a simple message box Private Sub DisplayADOError cn As Dim er As For Each er In MsgBox Number vbCrLf Source vbCrLf _ Text Next End Sub 320 Microsoft SQL Server 2005 Developer s Guide Figure 8-17 ADO error handling In this subroutine an ADO Connection object is passed in as a parameter. The ADO Errors collection is contained in the Connection object. Next a new ADO Error object named er is declared and a For Each loop iterates through the ADO Errors collection. The loop is required because the ADODB Errors collection can contain multiple Error objects where each Error object represents a .
đang nạp các trang xem trước