The IDataParameterCollection Interface


The IDataParameterCollection Interface

Each of the Command objects (SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand) exposed by a data adapter exposes a Parameters property to allow parameter values to be passed in at runtime. This saves the trouble of having to manually construct a string with runtime values and also provides type checking of the command parameters. The Parameters property returns a collection of the parameter objects that correspond to the particular data source (e.g., the XmlFileParameter created in Listing B.1). Therefore, for the .NET managed data provider for XML files, the XmlFileParameterCollection must exist to serve as the collection of XmlFileParameters. Listing B.2 shows how the XmlFileParameterCollection implements the IDataParameterCollection interface needed for the .NET managed data provider for XML files.

Listing B.2. XmlFileParameterCollection Implements IDataParameterCollection

[C#] public class XmlFileParameterCollection :               ArrayList, IDataParameterCollection {     public object this[string index]     {          get { return this[IndexOf(index)]; }          set {     this[IndexOf(index)] = value; }     }     public XmlFileParameterCollection() {}     public bool Contains(string parameterName)     {          return(-1 != IndexOf(parameterName));     }     public int IndexOf(string parameterName)     {          int index = 0;          foreach(XmlFileParameter item in this)          {               if (0 == _cultureAwareCompare                     (item.ParameterName, parameterName))               {                    return index;               }               index++;          }          return -1;     }     public void RemoveAt(string parameterName)     {          RemoveAt(IndexOf(parameterName));     }     public override int Add(object value)     {          return Add((XmlFileParameter)value);     }     public int Add(XmlFileParameter value)     {          if (((XmlFileParameter)value).ParameterName != null)          {               return base.Add(value);          }          else               throw new ArgumentException(SR.UnnamedParameter);     }     public int Add(string parameterName, DbType type)     {          return Add(new XmlFileParameter(parameterName, type));     }     public int Add(string parameterName, object value)     {          return Add(new XmlFileParameter(parameterName, value));     }     public int Add(string parameterName, DbType dbType,                    string sourceColumn)     {          return Add(new XmlFileParameter                     (parameterName, dbType, sourceColumn));     }     private int _cultureAwareCompare(string strA, string strB)     {          return CultureInfo.CurrentCulture.CompareInfo.Compare               (strA, strB, CompareOptions.IgnoreKanaType |               CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase);     } } [Visual Basic] Public Class XmlFileParameterCollection : Inherits ArrayList : _               Implements IDataParameterCollection     Public Default Property Item(ByVal index As String) As Object          Get               Return Me(IndexOf(index))          End Get          Set               Me(IndexOf(index)) = Value          End Set     End Property     Public Sub New()     End Sub     Public Function Contains(ByVal paramName As String) As Boolean _               Implements IDataParameterCollection.Contains          Return(-1 <> IndexOf(paramName))     End Function     Public Function IndexOf(ByVal paramName As String) As Integer _               Implements IDataParameterCollection.IndexOf          Dim index As Integer = 0          For Each item As XmlFileParameter In Me               If 0 = _cultureAwareCompare _                     (item.ParameterName, paramName) Then                    Return index               End If               index += 1          Next item          Return -1     End Function     Public Sub RemoveAt(ByVal paramName As String) _               Implements IDataParameterCollection.RemoveAt          RemoveAt(IndexOf(parameterName))     End Sub     Public Overrides Function Add(ByVal value As Object) As Integer          Return Add(CType(value, XmlFileParameter))     End Function     Public Function Add(ByVal value As XmlFileParameter) As Integer          If Not (CType(value, XmlFileParameter)).ParameterName _                     Is Nothing Then               Return MyBase.Add(value)          Else               Throw New ArgumentException(SR.UnnamedParameter)          End If     End Function     Public Function Add(ByVal paramName As String, _                     ByVal type As DbType) As Integer          Return Add(New XmlFileParameter(paramName, type))     End Function     Public Function Add(ByVal paramName As String,                     ByVal value As Object) As Integer          Return Add(New XmlFileParameter(paramName, value))     End Function     Public Function Add(ByVal paramName As String, _                     ByVal dbType As DbType, _                     ByVal sourceColumn As String) As Integer          Return Add(New XmlFileParameter _                     (paramName, dbType, sourceColumn))     End Function     Private Function _cultureAwareCompare _               (ByVal strA As String, ByVal strB As String) As Integer          Return CultureInfo.CurrentCulture.CompareInfo.Compare _                     (strA, strB, CompareOptions.IgnoreKanaType Or _                     CompareOptions.IgnoreWidth Or _                     CompareOptions.IgnoreCase)     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