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 () {
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;
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));
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)
234 bool Contains (object value)
237 //should not throw ArgumentNullException
239 if (!(value is OdbcParameter))
240 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
241 return Contains (((OdbcParameter) value).ParameterName);
248 bool Contains (string value)
250 if (value == null || value.Length == 0)
251 //should not throw ArgumentNullException
253 string value_upper = value.ToUpper ();
254 foreach (OdbcParameter p in this)
255 if (p.ParameterName.ToUpper ().Equals (value_upper))
264 void CopyTo (Array array, int index)
266 list.CopyTo (array, index);
273 IEnumerator GetEnumerator()
275 return list.GetEnumerator ();
282 int IndexOf (object value)
286 if (!(value is OdbcParameter))
287 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
288 return list.IndexOf (value);
295 int IndexOf (string parameterName)
297 if (parameterName == null || parameterName.Length == 0)
299 string parameterName_upper = parameterName.ToUpper ();
300 for (int i = 0; i < Count; i += 1)
301 if (this [i].ParameterName.ToUpper ().Equals (parameterName_upper))
310 void Insert (int index, object value)
313 throw new ArgumentNullException ("value");
314 if (!(value is OdbcParameter))
315 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
316 Insert (index, (OdbcParameter) value);
323 void Remove (object value)
326 throw new ArgumentNullException ("value");
327 if (!(value is OdbcParameter))
328 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
329 Remove ((OdbcParameter) value);
336 void RemoveAt (int index)
338 if (index >= list.Count || index < 0)
339 throw new IndexOutOfRangeException (String.Format ("Invalid index {0} for this OdbcParameterCollection with count = {1}", index, list.Count));
340 this [index].Container = null;
341 list.RemoveAt (index);
348 void RemoveAt (string parameterName)
350 RemoveAt (IndexOf (parameterName));
354 protected override DbParameter GetParameter (string name)
359 protected override DbParameter GetParameter (int index)
364 protected override void SetParameter (string name, DbParameter value)
366 this [name] = (OdbcParameter) value;
369 protected override void SetParameter (int index, DbParameter value)
371 this [index] = (OdbcParameter) value;
375 public override void AddRange (Array values)
378 throw new ArgumentNullException ("values");
379 foreach (OdbcParameter p in values)
381 throw new ArgumentNullException ("values", "The OdbcParameterCollection only accepts non-null OdbcParameter type objects");
382 // no need to check if parameter is already contained
383 foreach (OdbcParameter p in values)
387 public void AddRange (OdbcParameter [] values)
389 AddRange ((Array)values);
392 public void Insert (int index, OdbcParameter value)
394 if (index > list.Count || index < 0)
395 throw new ArgumentOutOfRangeException ("index", "The index must be non-negative and less than or equal to size of the collection");
397 throw new ArgumentNullException ("value");
398 if (value.Container != null)
399 throw new ArgumentException ("The OdbcParameter is already contained by another collection");
400 if (String.IsNullOrEmpty (value.ParameterName)) {
401 value.ParameterName = "Parameter" + nullParamCount;
404 value.Container = this;
405 list.Insert (index, value);
408 public OdbcParameter AddWithValue (string parameterName, Object value)
411 return Add (new OdbcParameter (parameterName, OdbcType.NVarChar));
412 return Add (new OdbcParameter (parameterName, value));
415 public void Remove (OdbcParameter value)
418 throw new ArgumentNullException ("value");
419 if (value.Container != this)
420 throw new ArgumentException ("values", "Attempted to remove an OdbcParameter that is not contained in this OdbcParameterCollection");
421 value.Container = null;
425 public bool Contains (OdbcParameter value)
428 //should not throw ArgumentNullException
430 if (value.Container != this)
432 return Contains (value.ParameterName);
435 public int IndexOf (OdbcParameter value)
438 //should not throw ArgumentNullException
440 return IndexOf ((Object) value);
443 public void CopyTo (OdbcParameter [] array, int index)
445 list.CopyTo (array, index);
449 #endregion // Methods