System.Data.Common.DataAdapter:RowUpdated& RowUpdating事件不存在 [英] System.Data.Common.DataAdapter: RowUpdated & RowUpdating Events not present

查看:99
本文介绍了System.Data.Common.DataAdapter:RowUpdated& RowUpdating事件不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个允许连接到多个提供商特定数据库的程序,所以显然我使用非提供者特定的类(在System.Data.Common中)来处理这些各种连接连接或数据适配器已通过使用提供程序的实现实例化。

I'm currently working on a program that allows for connections to multiple provider specific databases, so obviously I'm using the non-provider-specific classes (in System.Data.Common) to work with these various connections once the connection or data adapter has been instantiated through the use of the provider's implementation.

但是,我想利用据称是部分的RowUpdated和RowUpdating事件DataAdapter基类根据MSDN( http://msdn.microsoft.com/en-我们/库/ 6d1wk41s.aspx )。但是显然不是这样,因为这些事件只能通过DbDataAdapter的提供者特定的实现来实现(如SqlDataAdapter,OleDbDataAdapter等)。

However, I wanted to make use of the RowUpdated and RowUpdating events that are supposedly a part of the DataAdapter base class according to MSDN (http://msdn.microsoft.com/en-us/library/6d1wk41s.aspx). But obviously this is not the case, because those events are only implemented through the provider-specific implementations of DbDataAdapter (like SqlDataAdapter, OleDbDataAdapter, etc.).

我的主要目标可以通过使用通用的DbDataAdapter类来处理这些事件,而将数据转发给数据适配器的提供者特定实现的派生类将是一个很大的困难。

My main goal is to be able to handle those events through the use of the generic DbDataAdapter class, and it will be a big pain to have to cast things back to the derived classes of the provider-specific implementations for the data adapter.

这样做的任何光滑的想法?

Any slick ideas of doing this?

推荐答案

我在网上做了很多搜索,真的没有找到任何具体的东西,除了将实例重新导出到派生类以访问这些事件。

I did a lot of searching online, and really didn't find anything concrete besides just re-casting instances back to the derived classes to access those events.

所以,使用扩展方法,我添加DbDataAdapter抽象类的两个新方法将允许为这两个特定事件添加事件处理程序,这里是我的实现(编辑4/23/2013处理实例或静态处理程序ds):

So, with the use of Extension Methods, I added two new methods to the DbDataAdapter abstract class that will allow the adding of event handlers for those two specific events, and here is my implementation (edited 4/23/2013 for handling of instance or static handler methods):

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应该如何实现它们开始)。

I'm using the base RowUpdatedEventArgs & RowUpdatingEventArgs for the event arguments that get returned to the delegate, so if you need provider-specific members that are only available through the provider defined classes that are derived from the above two base event args classes, then they will need to be cast to those classes. Otherwise, it works fine, and now I have provider-independent event handlers available from the DbDataAdapter class (which is how Microsoft should have implemented them to begin with).

干杯!

这篇关于System.Data.Common.DataAdapter:RowUpdated&amp; RowUpdating事件不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆