事件处理程序的协同工作原理How Event Handlers Work Together

除非您正在 Visual Basic 进行编程,否则不管是否确实处理所有事件,都必须实现 连接 事件和 记录集 事件的所有事件处理程序。Unless you are programming in Visual Basic, all event handlers for Connection and Recordset events must be implemented, regardless of whether you actually process all of the events. 您必须执行的实现工作量取决于编程语言。The amount of implementation work you have to do depends on your programming language. 有关详细信息,请参阅 按语言的 ADO 事件实例化For more information, see ADO Event Instantiation by Language.

配对事件处理程序Paired Event Handlers

每个都将有一个关联的 完整 事件处理程序。Each Will event handler has an associated Complete event handler. 例如,当应用程序更改字段的值时,将调用 WillChangeField 事件处理程序。For example, when your application changes the value of a field, the WillChangeField event handler is called. 如果更改是可接受的,则应用程序保持 adStatus 参数不变,并执行操作。If the change is acceptable, your application leaves the adStatus parameter unchanged and the operation is performed. 操作完成后, FieldChangeComplete 事件会通知应用程序操作已完成。When the operation completes, a FieldChangeComplete event notifies your application that the operation has finished. 如果成功完成, adStatus 将包含 adStatusOK;否则, adStatus 将包含 adStatusErrorsOccurred ,必须检查 错误 对象以确定错误的原因。If it completed successfully, adStatus contains adStatusOK; otherwise, adStatus contains adStatusErrorsOccurred and you must check the Error object to determine the cause of the error.

调用 WillChangeField 时,你可能会确定不应进行此更改。When WillChangeField is called, you might determine that the change should not be made. 在这种情况下,将 adStatus 设置为 adStatusCancel。In that case, set adStatus to adStatusCancel. 操作已取消, FieldChangeComplete 事件接收到 adStatusErrorsOccurredadStatus 值。The operation is canceled and the FieldChangeComplete event receives an adStatus value of adStatusErrorsOccurred. Error 对象包含 AdErrOperationCancelled ,以便 FieldChangeComplete 处理程序知道该操作已取消。The Error object contains adErrOperationCancelled so that your FieldChangeComplete handler knows that the operation was canceled. 但是,您需要在更改 adStatus 参数之前检查其值,因为如果将 adStatus 设置为 " adStatusCantDeny",则将该参数设置为 " " 时,不会产生任何影响。However, you need to check the value of the adStatus parameter before changing it, because setting adStatus to adStatusCancel has no effect if the parameter was set to adStatusCantDeny on entry to the procedure.

有时操作可能会引发多个事件。Sometimes an operation can raise more than one event. 例如, Recordset 对象具有对 字段 更改和 记录 更改的成对事件。For example, the Recordset object has paired events for Field changes and Record changes. 当应用程序更改 字段 的值时,将调用 WillChangeField 事件处理程序。When your application changes the value of a Field, the WillChangeField event handler is called. 如果它确定操作可以继续,则也会引发 WillChangeRecord 事件处理程序。If it determines that the operation can continue, the WillChangeRecord event handler is also raised. 如果此处理程序还允许事件继续,则会进行更改并调用 FieldChangeCompleteRecordChangeComplete 事件处理程序。If this handler also allows the event to continue, the change is made and the FieldChangeComplete and RecordChangeComplete event handlers are called. 为特定操作调用事件处理程序的顺序未定义,因此应避免编写依赖于按特定顺序调用处理程序的代码。The order in which the Will event handlers for a particular operation are called is not defined, so you should avoid writing code that depends on calling handlers in a particular sequence.

在实例中,如果引发了多个事件,则其中一个事件可能会取消挂起的操作。In instances when multiple Will events are raised, one of the events might cancel the pending operation. 例如,当应用程序更改 字段 的值时,通常会调用 WillChangeFieldWillChangeRecord 事件处理程序。For example, when your application changes the value of a Field, both WillChangeField and WillChangeRecord event handlers would normally be called. 但是,如果在第一个事件处理程序中取消了该操作,则会立即通过 adStatusOperationCancelled 调用其关联的 完整 处理程序。However, if the operation is canceled in the first event handler, its associated Complete handler is immediately called with adStatusOperationCancelled. 从不调用第二个处理程序。The second handler is never called. 但是,如果第一个事件处理程序允许事件继续,则将调用另一个事件处理程序。If, however, the first event handler allows the event to proceed, the other event handler will be called. 如果它取消了该操作,则将按前面的示例中所示调用两个 完整 事件。If it then cancels the operation, both Complete events will be called as in the earlier examples.

未成对的事件处理程序Unpaired Event Handlers

只要传递到事件的状态不是 adStatusCantDeny,您可以通过在 status 参数中返回 adStatusUnwantedEvent 来关闭任何事件的事件通知。As long as the status passed to the event is not adStatusCantDeny, you can turn off event notifications for any event by returning adStatusUnwantedEvent in the Status parameter. 例如,第一次调用 完整 的事件处理程序时,可以返回 adStatusUnwantedEventFor example, when your Complete event handler is called the first time, you can return adStatusUnwantedEvent. 稍后 将只接收 事件。You will subsequently receive only Will events. 但是,某些事件可能会出于多种原因而触发。However, some events can be triggered for more than one reason. 在这种情况下,该事件将具有 Reason 参数。In that case, the event will have a Reason parameter. 当你返回 adStatusUnwantedEvent 时,只有在此事件发生时才会停止接收该事件的通知。When you return adStatusUnwantedEvent, you will stop receiving notifications for that event only when they occur for that particular reason. 换句话说,你可能会收到可能触发事件的每个可能原因的通知。In other words, you will potentially receive notification for each possible reason that the event could be triggered.

如果要检查将在操作中使用的参数 ,则可以使用 Single。Single Will event handlers can be useful when you want to examine the parameters that will be used in an operation. 您可以修改这些操作参数,也可以取消该操作。You can modify those operation parameters or cancel the operation.

或者,将 " 完成 事件通知" 保持为启用状态。Alternatively, leave Complete event notification enabled. 调用第一个事件处理程序时,将返回 adStatusUnwantedEventWhen your first Will event handler is called, return adStatusUnwantedEvent. 随后将仅接收 完整 事件。You will subsequently receive only Complete events.

单个 完整 事件处理程序对于管理异步操作非常有用。Single Complete event handlers can be useful for managing asynchronous operations. 每个异步操作都有一个适当的 完整 事件。Each asynchronous operation has an appropriate Complete event.

例如,填充大型 Recordset 对象可能需要很长时间。For example, it can take a long time to populate a large Recordset object. 如果你的应用程序已适当编写,你可以启动 Recordset.Open(...,adAsyncExecute) 操作并继续其他处理。If your application is appropriately written, you can start a Recordset.Open(...,adAsyncExecute) operation and continue with other processing. 记录集ExecuteComplete 事件填充时,最终会收到通知。You will eventually be notified when the Recordset is populated by an ExecuteComplete event.

单个事件处理程序和多个对象Single Event Handlers and Multiple Objects

编程语言(例如 Microsoft Visual C++®)的灵活性使您能够从多个对象处理一个事件处理程序事件。The flexibility of a programming language like Microsoft Visual C++® enables you to have one event handler process events from multiple objects. 例如,你可能有一个从多个 连接 对象的 "断开连接" 事件处理程序事件。For example, you could have one Disconnect event handler process events from several Connection objects. 如果某个连接结束,则会调用 " 断开连接 " 事件处理程序。If one of the connections ended, the Disconnect event handler would be called. 由于事件处理程序对象参数将设置为相应的 连接 对象,因此可以告知哪个连接引发了事件。You could tell which connection caused the event because the event-handler object parameter would be set to the corresponding Connection object.

备注

此方法不能在 Visual Basic 中使用,因为该语言只能将一个对象关联到一个事件处理程序。This technique cannot be used in Visual Basic because that language can correlate only one object to an event handler.

另请参阅See Also

ADO 事件处理程序摘要 ADO Event Handler Summary
按语言的 ADO 事件实例化 ADO Event Instantiation by Language
事件参数 Event Parameters
事件类型Types of Events