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;
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; }
130 object ICollection.SyncRoot {
132 public override object SyncRoot {
134 get { return list.SyncRoot; }
138 object IList.this[int index] {
139 get { return list[index]; }
140 set { list[index] = value; }
143 object IDataParameterCollection.this[string name]
145 get { return this[name]; }
147 if (!(value is OdbcParameter))
148 throw new InvalidCastException ("Only OdbcParameter objects can be used.");
149 this [name] = (OdbcParameter) value;
155 #endregion // Properties
160 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
166 int Add (object value)
168 if (!(value is OdbcParameter))
169 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
170 Add ((OdbcParameter) value);
171 return IndexOf (value);
174 public OdbcParameter Add (OdbcParameter parameter)
176 if (parameter.Container != null)
177 throw new ArgumentException ("The OdbcParameter specified in " +
178 "the value parameter is already " +
179 "added to this or another OdbcParameterCollection.");
180 if (parameter.ParameterName == null || parameter.ParameterName == "") {
181 parameter.ParameterName = "Parameter" + nullParamCount;
184 parameter.Container = this;
185 list.Add (parameter);
190 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
191 [Obsolete ("Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value).")]
193 public OdbcParameter Add (string name, object value)
195 return Add (new OdbcParameter (name, value));
198 public OdbcParameter Add (string name, OdbcType type)
200 return Add (new OdbcParameter (name, type));
203 public OdbcParameter Add (string name, OdbcType type, int width)
205 return Add (new OdbcParameter (name, type, width));
208 public OdbcParameter Add (string name, OdbcType type,
209 int width, string src_col)
211 return Add (new OdbcParameter (name, type, width, src_col));
214 internal void Bind (IntPtr hstmt)
216 for (int i = 0; i < Count; i++)
217 this [i].Bind (hstmt, i + 1);
226 foreach (OdbcParameter p in list)
236 bool Contains (object value)
239 //should not throw ArgumentNullException
241 if (!(value is OdbcParameter))
242 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
243 return Contains (((OdbcParameter) value).ParameterName);
250 bool Contains (string value)
252 if (value == null || value == "")
253 //should not throw ArgumentNullException
255 string value_upper = value.ToUpper ();
256 foreach (OdbcParameter p in this)
257 if (p.ParameterName.ToUpper ().Equals (value_upper))
266 void CopyTo (Array array, int index)
268 list.CopyTo (array, index);
275 IEnumerator GetEnumerator()
277 return list.GetEnumerator ();
284 int IndexOf (object value)
288 if (!(value is OdbcParameter))
289 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
290 return list.IndexOf (value);
297 int IndexOf (string parameterName)
299 if (parameterName == null || parameterName == "")
301 string parameterName_upper = parameterName.ToUpper ();
302 for (int i = 0; i < Count; i += 1)
303 if (this [i].ParameterName.ToUpper ().Equals (parameterName_upper))
312 void Insert (int index, object value)
315 throw new ArgumentNullException ("value");
316 if (!(value is OdbcParameter))
317 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
318 Insert (index, (OdbcParameter) value);
325 void Remove (object value)
328 throw new ArgumentNullException ("value");
329 if (!(value is OdbcParameter))
330 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
331 Remove ((OdbcParameter) value);
338 void RemoveAt (int index)
340 if (index >= list.Count || index < 0)
341 throw new IndexOutOfRangeException (String.Format ("Invalid index {0} for this OdbcParameterCollection with count = {1}", index, list.Count));
342 this [index].Container = null;
343 list.RemoveAt (index);
350 void RemoveAt (string parameterName)
352 RemoveAt (IndexOf (parameterName));
357 protected override DbParameter GetParameter (string name)
362 protected override DbParameter GetParameter (int index)
367 protected override void SetParameter (string name, DbParameter value)
369 this [name] = (OdbcParameter) value;
372 protected override void SetParameter (int index, DbParameter value)
374 this [index] = (OdbcParameter) value;
378 public override void AddRange (Array values)
381 throw new ArgumentNullException ("values");
382 foreach (OdbcParameter p in values) {
384 throw new ArgumentNullException ("values", "The OdbcParameterCollection only accepts non-null OdbcParameter type objects");
386 // no need to check if parameter is already contained
387 foreach (OdbcParameter p in values)
391 public void AddRange (OdbcParameter [] values)
393 AddRange ((Array)values);
396 public void Insert (int index, OdbcParameter value)
398 if (index > list.Count || index < 0)
399 throw new ArgumentOutOfRangeException ("index", "The index must be non-negative and less than or equal to size of the collection");
401 throw new ArgumentNullException ("value");
402 if (value.Container != null)
403 throw new ArgumentException ("The OdbcParameter is already contained by another collection");
404 if (String.IsNullOrEmpty (value.ParameterName)) {
405 value.ParameterName = "Parameter" + nullParamCount;
408 value.Container = this;
409 list.Insert (index, value);
412 public OdbcParameter AddWithValue (string parameterName, Object value)
415 return Add (new OdbcParameter (parameterName, OdbcType.NVarChar));
416 return Add (new OdbcParameter (parameterName, value));
419 public void Remove (OdbcParameter value)
422 throw new ArgumentNullException ("value");
423 if (value.Container != this)
424 throw new ArgumentException ("values", "Attempted to remove an OdbcParameter that is not contained in this OdbcParameterCollection");
425 value.Container = null;
429 public bool Contains (OdbcParameter value)
432 //should not throw ArgumentNullException
434 if (value.Container != this)
436 return Contains (value.ParameterName);
439 public int IndexOf (OdbcParameter value)
442 //should not throw ArgumentNullException
444 return IndexOf ((Object) value);
447 public void CopyTo (OdbcParameter [] array, int index)
449 list.CopyTo (array, index);
452 #endregion // Methods