System.Data.Common.DataAdapter: RowUpdated &没有RowUpdating事件

本文关键字:没有 RowUpdating 事件 RowUpdated Data Common DataAdapter System | 更新日期: 2023-09-27 18:07:20

我目前正在编写一个程序,该程序允许连接到多个特定于提供者的数据库,所以很明显,一旦连接或数据适配器通过使用提供者的实现实例化,我将使用非特定于提供者的类(在System.Data.Common中)来处理这些不同的连接。

然而,我想利用RowUpdated和RowUpdating事件,根据MSDN (http://msdn.microsoft.com/en-us/library/6d1wk41s.aspx),这些事件应该是DataAdapter基类的一部分。但显然情况并非如此,因为这些事件仅通过特定于提供程序的DbDataAdapter实现(如SqlDataAdapter、OleDbDataAdapter等)。

我的主要目标是能够通过使用泛型DbDataAdapter类来处理这些事件,并且必须将内容强制转换回数据适配器特定于提供程序实现的派生类将是一个很大的痛苦。

有什么好主意吗?

System.Data.Common.DataAdapter: RowUpdated &没有RowUpdating事件

我在网上做了很多搜索,除了将实例重新转换回派生类以访问这些事件之外,真的没有找到任何具体的内容。

因此,使用扩展方法,我向DbDataAdapter抽象类添加了两个新方法,允许为这两个特定事件添加事件处理程序,这是我的实现(编辑于2013年4月23日,用于处理实例或静态处理程序方法):

using System;
using System.Data.Common;
using System.Reflection;
namespace Extensions
{
    /// <summary>
    /// Delegate event handler used with the <c>DbDataAdapter.RowUpdated</c> event.
    /// </summary>
    public delegate void RowUpdatedEventHandler(object sender, RowUpdatedEventArgs e);
    /// <summary>
    /// Delegate event handler used with the <c>DbDataAdapter.RowUpdating</c> event.
    /// </summary>
    public delegate void RowUpdatingEventHandler(object sender, RowUpdatingEventArgs e);
    public static class DbDataAdapterExtension
    {
        private static EventInfo GetEvent(string eventName, Type type)
        {
            return type.GetEvent(eventName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        }
        /// <summary>
        /// Registers a <c>RowUpdatedEventHandler</c> with this instance's <c>RowUpdated</c> event.
        /// </summary>
        /// <param name="handler">The event handler to register for the event.</param>
        /// <returns><c>true</c> if the event handler was successfully registered, otherwise <c>false</c>.</returns>
        public static bool AddRowUpdatedHandler(this DbDataAdapter adapter, RowUpdatedEventHandler handler)
        {
            EventInfo updEvent = GetEvent("RowUpdated", adapter.GetType());
            if (updEvent != null)
            {
                try
                {
                    if (handler.Method.IsStatic)
                    {
                        updEvent.AddEventHandler(adapter, Delegate.CreateDelegate(updEvent.EventHandlerType, handler.Method));
                    }
                    else
                    {
                        updEvent.AddEventHandler(adapter, Delegate.CreateDelegate(updEvent.EventHandlerType, handler.Target, handler.Method));
                    }
                    return true;
                }
                catch { }
            }
            return false;
        }
        /// <summary>
        /// Registers a <c>RowUpdatingEventHandler</c> with this instance's <c>RowUpdating</c> event.
        /// </summary>
        /// <param name="handler">The event handler to register for the event.</param>
        /// <returns><c>true</c> if the event handler was successfully registered, otherwise <c>false</c>.</returns>
        public static bool AddRowUpdatingHandler(this DbDataAdapter adapter, RowUpdatingEventHandler handler)
        {
            EventInfo updEvent = GetEvent("RowUpdating", adapter.GetType());
            if (updEvent != null)
            {
                try
                {
                    if (handler.Method.IsStatic)
                    {
                        updEvent.AddEventHandler(adapter, Delegate.CreateDelegate(updEvent.EventHandlerType, handler.Method));
                    }
                    else
                    {
                        updEvent.AddEventHandler(adapter, Delegate.CreateDelegate(updEvent.EventHandlerType, handler.Target, handler.Method));
                    }
                    return true;
                }
                catch { }
            }
            return false;
        }
    }
}

我使用的是基础RowUpdatedEventArgs &RowUpdatingEventArgs用于返回给委托的事件参数,因此,如果您需要特定于提供程序的成员,这些成员只能通过从上述两个基本事件参数类派生的提供程序定义的类获得,则需要将它们强制转换为这些类。否则,它工作得很好,现在我有了独立于提供程序的事件处理程序,可以从DbDataAdapter类获得(这就是Microsoft最初应该实现它们的方式)。

干杯!

谢谢-非常有用!

也许有人在Visual Basic中需要这个

这样写,例如:

Imports YourProject.DbDataAdapterExtension

。..

Dim HandleOnRowUpd As RowUpdatedEventHandler = AddressOf OnRowUpdated
Dim TrueFalse As Boolean = YourDataAdapter.AddRowUpdatedHandler(HandleOnRowUpd)

Imports System.Data.Common
Imports System.Reflection
Imports System.Runtime.CompilerServices
' Namespace Extensions
''' <summary>
''' Delegate event handler used with the <c>DbDataAdapter.RowUpdated</c> event.
''' </summary>
''' 
Public Delegate Sub RowUpdatedEventHandler(sender As Object, e As RowUpdatedEventArgs)
''' <summary>
''' Delegate event handler used with the <c>DbDataAdapter.RowUpdating</c> event.
''' </summary>
''' 
Public Delegate Sub RowUpdatingEventHandler(sender As Object, e As RowUpdatingEventArgs)
Public Module DbDataAdapterExtension
    Sub New()
    End Sub
    Private Function GetEvent(eventName As String, type As Type) As EventInfo
        Return type.GetEvent(eventName, BindingFlags.[Public] Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
    End Function
    ''' <summary>
    ''' Registers a <c>RowUpdatedEventHandler</c> with this instance's <c>RowUpdated</c> event.
    ''' </summary>
    ''' <param name="handler">The event handler to register for the event.</param>
    ''' <returns><c>true</c> if the event handler was successfully registered, otherwise <c>false</c>.</returns>
    ''' 
    <Extension> Public Function AddRowUpdatedHandler(adapter As DbDataAdapter, handler As RowUpdatedEventHandler) As Boolean
        Dim updEvent As EventInfo = GetEvent("RowUpdated", adapter.[GetType]())
        If updEvent IsNot Nothing Then
            Try
                If handler.Method.IsStatic Then
                    updEvent.AddEventHandler(adapter, [Delegate].CreateDelegate(updEvent.EventHandlerType, handler.Method))
                Else
                    updEvent.AddEventHandler(adapter, [Delegate].CreateDelegate(updEvent.EventHandlerType, handler.Target, handler.Method))
                End If
                Return True
            Catch
            End Try
        End If
        Return False
    End Function
    ''' <summary>
    ''' Registers a <c>RowUpdatingEventHandler</c> with this instance's <c>RowUpdating</c> event.
    ''' </summary>
    ''' <param name="handler">The event handler to register for the event.</param>
    ''' <returns><c>true</c> if the event handler was successfully registered, otherwise <c>false</c>.</returns>
    ''' 
    <Extension> Public Function AddRowUpdatingHandler(adapter As DbDataAdapter, handler As RowUpdatingEventHandler) As Boolean
        Dim updEvent As EventInfo = GetEvent("RowUpdating", adapter.[GetType]())
        If updEvent IsNot Nothing Then
            Try
                If handler.Method.IsStatic Then
                    updEvent.AddEventHandler(adapter, [Delegate].CreateDelegate(updEvent.EventHandlerType, handler.Method))
                Else
                    updEvent.AddEventHandler(adapter, [Delegate].CreateDelegate(updEvent.EventHandlerType, handler.Target, handler.Method))
                End If
                Return True
            Catch
            End Try
        End If
        Return False
    End Function
End Module
' End Namespace