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;
38 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 ArrayList list = new ArrayList ();
55 int nullParamCount = 1;
61 internal OdbcParameterCollection () {
64 #endregion // Constructors
70 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
77 get { return list.Count; }
81 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
82 public new OdbcParameter this [int index] {
83 get { return (OdbcParameter) list[index]; }
84 set { list[index] = value; }
88 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
89 public new OdbcParameter this [string parameterName] {
91 foreach (OdbcParameter p in list)
92 if (p.ParameterName.Equals (parameterName))
94 throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
97 if (!Contains (parameterName))
98 throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
99 this [IndexOf (parameterName)] = value;
104 bool IList.IsFixedSize {
106 public override bool IsFixedSize {
108 get { return false; }
112 bool IList.IsReadOnly {
114 public override bool IsReadOnly {
116 get { return false; }
120 bool ICollection.IsSynchronized {
122 public override bool IsSynchronized {
124 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 name]
144 get { return this [name]; }
146 if (!(value is OdbcParameter))
147 throw new InvalidCastException ("Only OdbcParameter objects can be used.");
148 this [name] = (OdbcParameter) value;
154 #endregion // Properties
159 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
165 int Add (object value)
167 if (!(value is OdbcParameter))
168 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
169 Add ((OdbcParameter) value);
170 return IndexOf (value);
173 public OdbcParameter Add (OdbcParameter parameter)
175 if (parameter.Container != null)
176 throw new ArgumentException ("The OdbcParameter specified in " +
177 "the value parameter is already " +
178 "added to this or another OdbcParameterCollection.");
179 if (parameter.ParameterName == null || parameter.ParameterName == "") {
180 parameter.ParameterName = "Parameter" + nullParamCount;
183 parameter.Container = this;
184 list.Add (parameter);
189 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
190 [Obsolete ("Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value).")]
192 public OdbcParameter Add (string name, object value)
194 return Add (new OdbcParameter (name, value));
197 public OdbcParameter Add (string name, OdbcType type)
199 return Add (new OdbcParameter (name, type));
202 public OdbcParameter Add (string name, OdbcType type, int width)
204 return Add (new OdbcParameter (name, type, width));
207 public OdbcParameter Add (string name, OdbcType type,
208 int width, string src_col)
210 return Add (new OdbcParameter (name, type, width, src_col));
213 internal void Bind (IntPtr hstmt)
215 for (int i = 0; i < Count; i++)
216 this [i].Bind (hstmt, i + 1);
225 foreach (OdbcParameter p in list)
235 bool Contains (object value)
238 //should not throw ArgumentNullException
240 if (!(value is OdbcParameter))
241 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
242 return Contains (((OdbcParameter) value).ParameterName);
249 bool Contains (string value)
251 if (value == null || value == "")
252 //should not throw ArgumentNullException
254 string value_upper = value.ToUpper ();
255 foreach (OdbcParameter p in this)
256 if (p.ParameterName.ToUpper ().Equals (value_upper))
265 void CopyTo (Array array, int index)
267 list.CopyTo (array, index);
274 IEnumerator GetEnumerator()
276 return list.GetEnumerator ();
283 int IndexOf (object value)
287 if (!(value is OdbcParameter))
288 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
289 return list.IndexOf (value);
296 int IndexOf (string parameterName)
298 if (parameterName == null || parameterName == "")
300 string parameterName_upper = parameterName.ToUpper ();
301 for (int i = 0; i < Count; i += 1)
302 if (this [i].ParameterName.ToUpper ().Equals (parameterName_upper))
311 void Insert (int index, object value)
314 throw new ArgumentNullException ("value");
315 if (!(value is OdbcParameter))
316 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
317 Insert (index, (OdbcParameter) value);
324 void Remove (object value)
327 throw new ArgumentNullException ("value");
328 if (!(value is OdbcParameter))
329 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
330 Remove ((OdbcParameter) value);
337 void RemoveAt (int index)
339 if (index >= list.Count || index < 0)
340 throw new IndexOutOfRangeException (String.Format ("Invalid index {0} for this OdbcParameterCollection with count = {1}", index, list.Count));
341 this [index].Container = null;
342 list.RemoveAt (index);
349 void RemoveAt (string parameterName)
351 RemoveAt (IndexOf (parameterName));
356 protected override DbParameter GetParameter (string name)
361 protected override DbParameter GetParameter (int index)
366 protected override void SetParameter (string name, DbParameter value)
368 this [name] = (OdbcParameter) value;
371 protected override void SetParameter (int index, DbParameter value)
373 this [index] = (OdbcParameter) value;
377 public override void AddRange (Array values)
380 throw new ArgumentNullException ("values");
381 foreach (OdbcParameter p in values) {
383 throw new ArgumentNullException ("values", "The OdbcParameterCollection only accepts non-null OdbcParameter type objects");
385 // no need to check if parameter is already contained
386 foreach (OdbcParameter p in values)
390 public void AddRange (OdbcParameter [] values)
392 AddRange ((Array)values);
395 public void Insert (int index, OdbcParameter value)
397 if (index > list.Count || index < 0)
398 throw new ArgumentOutOfRangeException ("index", "The index must be non-negative and less than or equal to size of the collection");
400 throw new ArgumentNullException ("value");
401 if (value.Container != null)
402 throw new ArgumentException ("The OdbcParameter is already contained by another collection");
403 if (String.IsNullOrEmpty (value.ParameterName)) {
404 value.ParameterName = "Parameter" + nullParamCount;
407 value.Container = this;
408 list.Insert (index, value);
411 public OdbcParameter AddWithValue (string parameterName, Object value)
414 return Add (new OdbcParameter (parameterName, OdbcType.NVarChar));
415 return Add (new OdbcParameter (parameterName, value));
418 public void Remove (OdbcParameter value)
421 throw new ArgumentNullException ("value");
422 if (value.Container != this)
423 throw new ArgumentException ("values", "Attempted to remove an OdbcParameter that is not contained in this OdbcParameterCollection");
424 value.Container = null;
428 public bool Contains (OdbcParameter value)
431 //should not throw ArgumentNullException
433 if (value.Container != this)
435 return Contains (value.ParameterName);
438 public int IndexOf (OdbcParameter value)
441 //should not throw ArgumentNullException
443 return IndexOf ((Object) value);
446 public void CopyTo (OdbcParameter [] array, int index)
448 list.CopyTo (array, index);
451 #endregion // Methods