Implementazione di un DataAdapter
Se si implementa l'insieme completo di interfacce del provider di dati .NET Framework, per l'implementazione di DataAdapter saranno necessarie poche righe di codice, poiché la maggior parte dell'implementazione viene fornita dalla classe System.Data.Common.DbDataAdapter.
Per utilizzare DbDataAdapter
Fornire implementazioni di IDbConnection, IDbCommand, IDataReader e così via. Tali componenti vengono utilizzati dalla classe DbDataAdapter nell'implementazione.
Creare una classe che erediti da DbDataAdapter e IDbDataAdapter. Esempio:
Public Class TemplateDataAdapter Inherits DbDataAdapter Implements IDbDataAdapter End Class [C#] public class TemplateDataAdapter : DbDataAdapter, IDbDataAdapter { }
Implementare l'interfaccia IDbDataAdapter e fornire implementazioni di metodi e proprietà che forniscono la "tipizzazione in modo sicuro". In tal modo si consentirà agli utenti del provider di dati .NET Framework di utilizzare riferimenti diretti agli oggetti forniti, anziché utilizzare interfacce quali IDbCommand.
Nell'esempio seguente viene riportata una proprietà SelectCommand, che restituisce un TemplateCommand tipizzato in modo sicuro. Quando si esegue il cast del TemplateDataAdapter in un IDbDataAdapter, la proprietà TemplateDataAdapter.SelectCommand restituirà un 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; } }
Implementare versioni di RowUpdatedEventArgs e RowUpdatingEventArgs specifiche per il provider e implementare i tipi di gestori di eventi associati (si tratta di codice standard). Per fornire la tipizzazione in modo sicuro, sono disponibili anche i tipi di evento in overload, che consentono l'esposizione con tipizzazione in modo sicuro dell'oggetto evento stesso e delle proprietà appropriate (quale la proprietà Command). Esempio:
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; } } }
Implementare i metodi astratti di DbDataAdapter. Esempio:
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); } }
Completare l'implementazione degli eventi nella classe derivata DbDataAdapter, come illustrato nell'esempio seguente. Si noti che gli eventi fanno parte della classe TemplateDataAdapter e che i delegati fanno parte dello spazio dei nomi.
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);
Negli argomenti seguenti viene riportato del codice di esempio per l'implementazione di un oggetto DataAdapter.
Per un esempio di implementazione in Visual Basic:
Per un esempio di implementazione in C#:
Vedere anche
Implementazione di un provider di dati .NET Framework | Esempio di provider di dati .NET Framework