2 // System.Data.Odbc.OdbcParameterCollection
5 // Brian Ritchie (brianlritchie@hotmail.com)
6 // Umadevi S (sumadevi@novell.com)
7 // Amit Biswas (amit@amitbiswas.com)
9 // Copyright (C) Brian Ritchie, 2002
10 // Copyright (C) Novell,Inc
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.ComponentModel;
39 using System.Data.Common;
41 namespace System.Data.Odbc
43 [ListBindable (false)]
44 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
46 public sealed class OdbcParameterCollection : DbParameterCollection
48 public sealed class OdbcParameterCollection : MarshalByRefObject,
49 IDataParameterCollection, IList, ICollection, IEnumerable
54 readonly ArrayList list = new ArrayList ();
55 int nullParamCount = 1;
61 internal OdbcParameterCollection ()
65 #endregion // Constructors
71 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
78 get { return list.Count; }
82 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
83 public new OdbcParameter this [int index] {
84 get { return (OdbcParameter) list [index]; }
85 set { list [index] = value; }
89 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
90 public new OdbcParameter this [string parameterName] {
92 foreach (OdbcParameter p in list)
93 if (p.ParameterName.Equals (parameterName))
95 throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
98 if (!Contains (parameterName))
99 throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
100 this [IndexOf (parameterName)] = value;
105 bool IList.IsFixedSize {
107 public override bool IsFixedSize {
109 get { return false; }
113 bool IList.IsReadOnly {
115 public override bool IsReadOnly {
117 get { return false; }
121 bool ICollection.IsSynchronized {
123 public override bool IsSynchronized {
125 get { return list.IsSynchronized; }
129 object ICollection.SyncRoot {
131 public override object SyncRoot {
133 get { return list.SyncRoot; }
137 object IList.this [int index] {
138 get { return list [index]; }
139 set { list [index] = value; }
142 object IDataParameterCollection.this [string index]
144 get { return this [index]; }
146 if (!(value is OdbcParameter))
147 throw new InvalidCastException ("Only OdbcParameter objects can be used.");
148 this [index] = (OdbcParameter) value;
153 #endregion // Properties
158 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
164 int Add (object value)
166 if (!(value is OdbcParameter))
167 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
168 Add ((OdbcParameter) value);
169 return IndexOf (value);
172 public OdbcParameter Add (OdbcParameter value)
174 if (value.Container != null)
175 throw new ArgumentException ("The OdbcParameter specified in " +
176 "the value parameter is already " +
177 "added to this or another OdbcParameterCollection.");
178 if (value.ParameterName == null || value.ParameterName.Length == 0) {
179 value.ParameterName = "Parameter" + nullParamCount;
182 value.Container = this;
188 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
189 [Obsolete ("Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value).")]
191 public OdbcParameter Add (string parameterName, object value)
193 return Add (new OdbcParameter (parameterName, value));
196 public OdbcParameter Add (string parameterName, OdbcType odbcType)
198 return Add (new OdbcParameter (parameterName, odbcType));
201 public OdbcParameter Add (string parameterName, OdbcType odbcType, int size)
203 return Add (new OdbcParameter (parameterName, odbcType, size));
206 public OdbcParameter Add (string parameterName, OdbcType odbcType,
207 int size, string sourceColumn)
209 return Add (new OdbcParameter (parameterName, odbcType,
210 size, sourceColumn));
219 foreach (OdbcParameter p in list)
228 bool Contains (object value)
231 //should not throw ArgumentNullException
233 if (!(value is OdbcParameter))
234 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
235 return Contains (((OdbcParameter) value).ParameterName);
242 bool Contains (string value)
244 if (value == null || value.Length == 0)
245 //should not throw ArgumentNullException
247 string value_upper = value.ToUpper ();
248 foreach (OdbcParameter p in this)
249 if (p.ParameterName.ToUpper ().Equals (value_upper))
258 void CopyTo (Array array, int index)
260 list.CopyTo (array, index);
267 IEnumerator GetEnumerator()
269 return list.GetEnumerator ();
276 int IndexOf (object value)
280 if (!(value is OdbcParameter))
281 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
282 return list.IndexOf (value);
289 int IndexOf (string parameterName)
291 if (parameterName == null || parameterName.Length == 0)
293 string parameterName_upper = parameterName.ToUpper ();
294 for (int i = 0; i < Count; i += 1)
295 if (this [i].ParameterName.ToUpper ().Equals (parameterName_upper))
304 void Insert (int index, object value)
307 throw new ArgumentNullException ("value");
308 if (!(value is OdbcParameter))
309 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
310 Insert (index, (OdbcParameter) value);
317 void Remove (object value)
320 throw new ArgumentNullException ("value");
321 if (!(value is OdbcParameter))
322 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
323 Remove ((OdbcParameter) value);
330 void RemoveAt (int index)
332 if (index >= list.Count || index < 0)
333 throw new IndexOutOfRangeException (String.Format ("Invalid index {0} for this OdbcParameterCollection with count = {1}", index, list.Count));
334 this [index].Container = null;
335 list.RemoveAt (index);
342 void RemoveAt (string parameterName)
344 RemoveAt (IndexOf (parameterName));
348 protected override DbParameter GetParameter (string name)
353 protected override DbParameter GetParameter (int index)
358 protected override void SetParameter (string name, DbParameter value)
360 this [name] = (OdbcParameter) value;
363 protected override void SetParameter (int index, DbParameter value)
365 this [index] = (OdbcParameter) value;
369 public override void AddRange (Array values)
372 throw new ArgumentNullException ("values");
373 foreach (OdbcParameter p in values)
375 throw new ArgumentNullException ("values", "The OdbcParameterCollection only accepts non-null OdbcParameter type objects");
376 // no need to check if parameter is already contained
377 foreach (OdbcParameter p in values)
381 public void AddRange (OdbcParameter [] values)
383 AddRange ((Array) values);
386 public void Insert (int index, OdbcParameter value)
388 if (index > list.Count || index < 0)
389 throw new ArgumentOutOfRangeException ("index", "The index must be non-negative and less than or equal to size of the collection");
391 throw new ArgumentNullException ("value");
392 if (value.Container != null)
393 throw new ArgumentException ("The OdbcParameter is already contained by another collection");
394 if (String.IsNullOrEmpty (value.ParameterName)) {
395 value.ParameterName = "Parameter" + nullParamCount;
398 value.Container = this;
399 list.Insert (index, value);
402 public OdbcParameter AddWithValue (string parameterName, Object value)
405 return Add (new OdbcParameter (parameterName, OdbcType.NVarChar));
406 return Add (new OdbcParameter (parameterName, value));
409 public void Remove (OdbcParameter value)
412 throw new ArgumentNullException ("value");
413 if (value.Container != this)
414 throw new ArgumentException ("values", "Attempted to remove an OdbcParameter that is not contained in this OdbcParameterCollection");
415 value.Container = null;
419 public bool Contains (OdbcParameter value)
422 //should not throw ArgumentNullException
424 if (value.Container != this)
426 return Contains (value.ParameterName);
429 public int IndexOf (OdbcParameter value)
432 //should not throw ArgumentNullException
434 return IndexOf ((Object) value);
437 public void CopyTo (OdbcParameter [] array, int index)
439 list.CopyTo (array, index);
443 #endregion // Methods