The IDbDataAdapter Interface


The IDbDataAdapter Interface

DataAdapters represent a set of commands and a database connection that is used to fill DataSets and reconcile changes made to the DataSets with the data source. They represent the bridge between a data source and a DataSet. The IDbDataAdapter interface and, optionally, the utility class DbDataAdapter make it easier for an inheriting class to create a DataAdapter. Like the IDbCommand and IDbConnection interfaces, classes that inherit IDbDataAdapter may define additional members to add provider-specific functionality. The XmlFileDataAdapter represents a set of data commands and a database connection that are used to fill the DataSets and update an XML File, but it doesn't include any additional provider-specific functionality. Listing B.6 provides an extract of the code for the XmlFileDataAdapter.

Listing B.6. XmlFileDataAdapter Implements IDbDataAdapter

[C#] public class XmlFileDataAdapter : DbDataAdapter, IDbDataAdapter {     XmlFileCommand selectCommand;     XmlFileCommand insertCommand;     XmlFileCommand updateCommand;     XmlFileCommand deleteCommand;     private bool disposed;     private static readonly object EventRowUpdated =           new object();     private static readonly object EventRowUpdating =           new object();     public XmlFileDataAdapter() { disposed = false; }     public XmlFileDataAdapter(XmlFileCommand cmd)  : this()     {          selectCommand = cmd;     }     public XmlFileDataAdapter           (String strCommand, XmlFileConnection conn) : this()     {          selectCommand = new XmlFileCommand(strCommand, conn);     }     public IDbCommand UpdateCommand     {          get { return updateCommand; }          set          {               if (value.GetType() == typeof(XmlFileCommand))                    updateCommand = (XmlFileCommand)value;               else                    throw new ApplicationException                     (SR.ExceptionMustBeXmlFileCmmands);          }     }     public IDbCommand SelectCommand     {          get { return selectCommand; }          set          {               if (value.GetType() == typeof(XmlFileCommand))                    selectCommand = (XmlFileCommand)value;               else                    throw new ApplicationException                          (SR.ExceptionMustBeXmlFileCmmands);          }     }     public IDbCommand DeleteCommand     {          get { return deleteCommand; }          set          {               if (value.GetType() == typeof(XmlFileCommand))                    deleteCommand = (XmlFileCommand)value;               else                    throw new ApplicationException                          (SR.ExceptionMustBeXmlFileCmmands);          }     }     public IDbCommand InsertCommand     {          get { return insertCommand; }          set          {               if (value.GetType() == typeof(XmlFileCommand))                    insertCommand = (XmlFileCommand)value;               else                    throw new ApplicationException                          (SR.ExceptionMustBeXmlFileCmmands);          }     }     public new int Update(DataSet dataSet)     {          dataSet.WriteXml(this.selectCommand.CommandText);          return 0;     }     public new int Update(DataSet dataSet, string srcTable)     {          if (dataSet.Tables[srcTable] ==                    dataSet.Tables[dataSet.Tables.Count])          dataSet.WriteXml(this.selectCommand.CommandText);               return 0;     }     override protected RowUpdatedEventArgs CreateRowUpdatedEvent           (DataRow dataRow, IDbCommand command,               StatementType statementType,               DataTableMapping tableMapping)     {          return new XmlFileRowUpdatedEventArgs                (dataRow, command, statementType, tableMapping);     }     override protected RowUpdatingEventArgs          CreateRowUpdatingEvent(DataRow dataRow,          IDbCommand command, StatementType statementType,          DataTableMapping tableMapping)     {          return new XmlFileRowUpdatingEventArgs                (dataRow, command, statementType, tableMapping);     } ... } [Visual Basic] Public Class XmlFileDataAdapter : Inherits DbDataAdapter _               : Implements IDbDataAdapter     Private _selectCommand As XmlFileCommand     Private _insertCommand As XmlFileCommand     Private _updateCommand As XmlFileCommand     Private _deleteCommand As XmlFileCommand     Private disposed As Boolean     Private Shared ReadOnly EventRowUpdated As Object = _          New Object()     Private Shared ReadOnly EventRowUpdating As Object = _          New Object()     Public Sub New()          disposed = False     End Sub     Public Sub New(ByVal cmd As XmlFileCommand)          Me.New()          _selectCommand = cmd     End Sub     Public Sub New(ByVal strCommand As String, _               ByVal conn As XmlFileConnection)          Me.New()         _selectCommand = _               New XmlFileCommand(strCommand, conn)     End Sub     Public Property UpdateCommand() As IDbCommand _               Implements IDbDataAdapter.UpdateCommand          Get               Return _updateCommand          End Get          Set             If Value.GetType() Is GetType(XmlFileCommand) Then                    _updateCommand = _                    CType(Value, XmlFileCommand)               Else                    Throw New _                         ApplicationException _                         (SR.ExceptionMustBeXmlFileCmmands)               End If          End Set     End Property     Public Property SelectCommand() As IDbCommand _               Implements IDbDataAdapter.SelectCommand          Get               Return _selectCommand          End Get          Set             If Value.GetType() Is GetType(XmlFileCommand) Then               _selectCommand = CType(Value, XmlFileCommand)             Else               Throw New ApplicationException _                     (SR.ExceptionMustBeXmlFileCmmands)             End If          End Set     End Property     Public Property DeleteCommand() As IDbCommand _               Implements IDbDataAdapter.DeleteCommand          Get               Return _deleteCommand          End Get          Set             If Value.GetType() Is GetType(XmlFileCommand) Then               _deleteCommand = CType(Value, XmlFileCommand)             Else                  Throw New ApplicationException _                     (SR.ExceptionMustBeXmlFileCmmands)             End If          End Set     End Property     Public Property InsertCommand() As IDbCommand _               Implements IDbDataAdapter.InsertCommand          Get               Return _insertCommand          End Get          Set             If Value.GetType() Is GetType(XmlFileCommand) Then               _insertCommand = CType(Value, XmlFileCommand)             Else               Throw New ApplicationException _                     (SR.ExceptionMustBeXmlFileCmmands)             End If          End Set     End Property     Public Shadows Function Update(ByVal dataSet As DataSet) _                    As Integer          dataSet.WriteXml(Me._selectCommand.CommandText)          Return 0     End Function     Public Shadows Function Update(ByVal dataSet As DataSet, _                ByVal srcTable As String) As Integer          If dataSet.Tables(srcTable) = _               dataSet.Tables(dataSet.Tables.Count) Then                    dataSet.WriteXml _               (Me._selectCommand.CommandText)          End If          Return 0     End Function     Overrides Protected Function CreateRowUpdatedEvent _           (ByVal dataRow As DataRow, ByVal command As IDbCommand, _            ByVal statementType As StatementType, ByVal tableMapping _            As DataTableMapping) As RowUpdatedEventArgs               Return New XmlFileRowUpdatedEventArgs _                     (dataRow, command, statementType, tableMapping)     End Function     Overrides Protected Function CreateRowUpdatingEvent _         (ByVal dataRow As DataRow, ByVal command As IDbCommand, _         ByVal statementType As StatementType, ByVal tableMapping _         As DataTableMapping) As RowUpdatingEventArgs               Return New XmlFileRowUpdatingEventArgs _                (dataRow, command, statementType, tableMapping)     End Function     ... End Class




Fenster Effective Use of Microsoft Enterprise Library(c) Building Blocks for Creating Enterprise Applications and Services 2006
Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications and Services
ISBN: 0321334213
EAN: 2147483647
Year: 2004
Pages: 103
Authors: Len Fenster

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net