共用方式為


實作 DataAdapter

如果您要實作完整的 .NET Framework 資料提供者介面集,則因為 System.Data.Common.DbDataAdapter 類別提供了大部分的實作,所以實作 DataAdapter 所需撰寫的程式碼很少。

若要使用 DbDataAdapter

  1. 提供 IDbConnectionIDbCommandIDataReader 等的實作。DbDataAdapter 類別在實作中使用這些元件。

  2. 建立從 DbDataAdapterIDbDataAdapter 繼承的類別。例如:

    Public Class TemplateDataAdapter
      Inherits DbDataAdapter
      Implements IDbDataAdapter
    End Class
    [C#]
    public class TemplateDataAdapter : DbDataAdapter, IDbDataAdapter
    {
    }
    
  3. 實作 IDbDataAdapter 介面,並提供能供應「強型別」的屬性和方法實作。如此可讓使用您的 .NET Framework 資料提供者的使用者直接參考您提供的物件,而不必透過 IDbCommand 等介面。

    下列範例顯示傳回強型別 TemplateCommand 的 SelectCommand 屬性。TemplateDataAdapter 當作 IDbDataAdapter 時,TemplateDataAdapter.SelectCommand 屬性會傳回 IDbCommand

    Private m_selectCommand As TemplateCommand
    
    Property IDbDataAdapterSelectCommand As IDbCommand Implements IDbDataAdapter.SelectCommand
      Get
        Return m_selectCommand
      End Get
      Set
        m_selectCommand = CType(value, TemplateCommand)
      End Set
    End Property
    
    Public Property SelectCommand As TemplateCommand
      Get
        Return m_selectCommand
      End Get
      Set
        m_selectCommand = value
      End Set
    End Property
    [C#]
    private TemplateCommand m_selectCommand;
    
    public TemplateCommand SelectCommand 
    {
      get { return m_selectCommand; }
      set { m_selectCommand = value; }
    }
    
    IDbCommand IDbDataAdapter.SelectCommand 
    {
      get { return m_selectCommand; }
      set { m_selectCommand = (TemplateCommand)value; }
    }
    
  4. 實作提供者專用的 RowUpdatedEventArgsRowUpdatingEventArgs 版本,以及相關的事件處理常式型別 (此為樣板程式碼)。也有多載事件類型可提供強型別,使事件物件本身和適當的屬性 (例如 Command 屬性) 以強型別的方式公開。例如:

    Public Class TemplateRowUpdatingEventArgs
      Inherits RowUpdatingEventArgs
    
      Public Sub New(row As DataRow, command As IDbCommand, statementType As StatementType, tableMapping As DataTableMapping)
                     MyBase.New(row, command, statementType, tableMapping)
      End Sub
    
      ' Hide the inherited implementation of the command property.
      Public Shadows Property Command As TemplateCommand
        Get
          Return CType(MyBase.Command, TemplateCommand)
        End Get
        Set
          MyBase.Command = value
        End Set
      End Property
    End Class
    
    Public Class TemplateRowUpdatedEventArgs
      Inherits RowUpdatedEventArgs
    
      Public Sub New(row As DataRow, command As IDbCommand, statementType As StatementType, tableMapping As DataTableMapping)
        MyBase.New(row, command, statementType, tableMapping) 
      End Sub
    
      ' Hide the inherited implementation of the command property.
      Public Shadows ReadOnly Property Command As TemplateCommand 
        Get
          Return CType(MyBase.Command, TemplateCommand)
        End Get
      End Property
    End Class
    [C#]
    public class TemplateRowUpdatingEventArgs : RowUpdatingEventArgs
    {
      public TemplateRowUpdatingEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) 
        : base(row, command, statementType, tableMapping) 
      {
      }
    
      // Hide the inherited implementation of the command property.
      new public TemplateCommand Command
      {
        get  { return (TemplateCommand)base.Command; }
        set  { base.Command = value; }
      }
    }
    
    public class TemplateRowUpdatedEventArgs : RowUpdatedEventArgs
    {
      public TemplateRowUpdatedEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
        : base(row, command, statementType, tableMapping) 
      {
      }
    
      // Hide the inherited implementation of the command property.
      new public TemplateCommand Command
      {
        get  { return (TemplateCommand)base.Command; }
      }
    }
    
  5. 實作 DbDataAdapter 的抽象方法。例如:

    Protected Overrides Function CreateRowUpdatedEvent(dataRow As DataRow, command As IDbCommand, statementType As StatementType, tableMapping As DataTableMapping) As RowUpdatedEventArgs
      Return New TemplateRowUpdatedEventArgs(dataRow, command, statementType, tableMapping)
    End Function
    
    Protected Overrides Function CreateRowUpdatingEvent(dataRow As DataRow, command As IDbCommand, statementType As StatementType, tableMapping As DataTableMapping) As RowUpdatingEventArgs
      Return New TemplateRowUpdatingEventArgs(dataRow, command, statementType, tableMapping)
    End Function
    
    Protected Overrides Sub OnRowUpdating(value As RowUpdatingEventArgs)
      Dim handler As TemplateRowUpdatingEventHandler  = CType(Events(EventRowUpdating), TemplateRowUpdatingEventHandler)
      If Not handler Is Nothing And value.GetType() Is Type.GetType("TemplateRowUpdatingEventArgs") Then 
        handler(Me, CType(value, TemplateRowUpdatingEventArgs))
      End If
    End Sub
    
    Protected Overrides Sub OnRowUpdated(value As RowUpdatedEventArgs)
      Dim handler As TemplateRowUpdatedEventHandler  = CType(Events(EventRowUpdated), TemplateRowUpdatedEventHandler)
      If Not handler Is Nothing And value.GetType() Is Type.GetType("TemplateRowUpdatedEventArgs") Then 
        handler(Me, CType(value, TemplateRowUpdatedEventArgs))
      End If
    End Sub
    [C#]
    override protected RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
    {
      return new TemplateRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
    }
    
    override protected RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
    {
      return new TemplateRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
    }
    
    override protected void OnRowUpdating(RowUpdatingEventArgs value)
    {
      TemplateRowUpdatingEventHandler handler = (TemplateRowUpdatingEventHandler) Events[EventRowUpdating];
      if ((null != handler) && (value is TemplateRowUpdatingEventArgs)) 
      {
        handler(this, (TemplateRowUpdatingEventArgs) value);
      }
    }
    
    override protected void OnRowUpdated(RowUpdatedEventArgs value)
    {
      TemplateRowUpdatedEventHandler handler = (TemplateRowUpdatedEventHandler) Events[EventRowUpdated];
      if ((null != handler) && (value is TemplateRowUpdatedEventArgs)) 
      {
        handler(this, (TemplateRowUpdatedEventArgs) value);
      }
    }
    
  6. 如下列範例所示,完成 DbDataAdapter 衍生類別 (Derived Class) 上的事件實作。請注意,這些事件是 TemplateDataAdapter 類別的一部份,且委派 (Delegate) 是命名空間的一部份。

    Public Event RowUpdating As TemplateRowUpdatingEventHandler 
    Public Event RowUpdated As TemplateRowUpdatedEventHandler 
    
    Public Delegate Sub TemplateRowUpdatingEventHandler(sender As Object, e As TemplateRowUpdatingEventArgs)
    Public Delegate Sub TemplateRowUpdatedEventHandler(sender As Object, e As TemplateRowUpdatedEventArgs)
    [C#]
    public event TemplateRowUpdatingEventHandler RowUpdating
    {
      add { Events.AddHandler(EventRowUpdating, value); }
      remove { Events.RemoveHandler(EventRowUpdating, value); }
    }
    
    public event TemplateRowUpdatedEventHandler RowUpdated
    {
      add { Events.AddHandler(EventRowUpdated, value); }
      remove { Events.RemoveHandler(EventRowUpdated, value); }
    }
    
    public delegate void TemplateRowUpdatingEventHandler(object sender, TemplateRowUpdatingEventArgs e);
    public delegate void TemplateRowUpdatedEventHandler(object sender, TemplateRowUpdatedEventArgs e);
    

下列主題包含實作 DataAdapter 物件的範例程式碼。

如需 Visual Basic 實作範例:

如需 C# 實作範例:

請參閱

實作 .NET Framework 資料提供者 | .NET Framework 資料提供者範例