2 // System.Data.SqlClient.SqlParameterCollection.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
8 // Diego Caravana (diego@toth.it)
9 // Umadevi S (sumadevi@novell.com)
11 // (C) Ximian, Inc 2002
12 // Copyright (C) Tim Coleman, 2002
16 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using System.Collections;
40 using System.ComponentModel;
42 using System.Data.Common;
46 namespace System.Data.SqlClient
48 [ListBindable (false)]
49 [Editor ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, " + Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
51 public sealed class SqlParameterCollection : DbParameterCollection, IDataParameterCollection, IList, ICollection, IEnumerable
53 public sealed class SqlParameterCollection : MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
58 ArrayList list = new ArrayList();
59 TdsMetaParameterCollection metaParameters;
66 internal SqlParameterCollection (SqlCommand command)
68 this.command = command;
69 metaParameters = new TdsMetaParameterCollection ();
72 #endregion // Constructors
76 #if ONLY_1_1 || ONLY_1_0
78 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
85 get { return list.Count; }
89 public override bool IsFixedSize {
91 return list.IsFixedSize;
95 public override bool IsReadOnly {
97 return list.IsReadOnly;
101 public override bool IsSynchronized {
103 return list.IsSynchronized;
107 public override object SyncRoot {
109 return list.SyncRoot;
113 object IList.this [int index] {
114 get { return (SqlParameter) this [index]; }
115 set { this [index] = (SqlParameter) value; }
118 bool IList.IsFixedSize {
119 get { return list.IsFixedSize; }
122 bool IList.IsReadOnly {
123 get { return list.IsReadOnly; }
126 bool ICollection.IsSynchronized {
127 get { return list.IsSynchronized; }
130 object ICollection.SyncRoot {
131 get { return list.SyncRoot; }
134 object IDataParameterCollection.this [string index] {
135 get { return this [index]; }
137 if (!(value is SqlParameter))
138 throw new InvalidCastException ("Only SQLParameter objects can be used.");
139 this [index] = (SqlParameter) value;
145 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
150 SqlParameter this [int index] {
152 if (index < 0 || index >= list.Count)
153 throw new IndexOutOfRangeException ("The specified index is out of range.");
154 return (SqlParameter) list [index];
157 if (index < 0 || index >= list.Count)
158 throw new IndexOutOfRangeException ("The specified index is out of range.");
159 list [index] = (SqlParameter) value;
164 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
169 SqlParameter this [string parameterName] {
171 foreach (SqlParameter p in list)
172 if (p.ParameterName.Equals (parameterName))
174 throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
177 if (!Contains (parameterName))
178 throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
179 this [IndexOf (parameterName)] = value;
184 protected override DbParameter GetParameter (int index)
189 protected override DbParameter GetParameter (string parameterName)
191 return this [parameterName];
194 protected override void SetParameter (int index, DbParameter value)
196 this [index] = (SqlParameter) value;
199 protected override void SetParameter (string parameterName, DbParameter value)
201 this [parameterName] = (SqlParameter) value;
205 internal TdsMetaParameterCollection MetaParameters {
206 get { return metaParameters; }
209 #endregion // Properties
214 [EditorBrowsable (EditorBrowsableState.Never)]
220 int Add (object value)
222 if (!(value is SqlParameter))
223 throw new InvalidCastException ("The parameter was not an SqlParameter.");
224 Add ((SqlParameter) value);
225 return IndexOf (value);
228 public SqlParameter Add (SqlParameter value)
230 if (value.Container != null)
231 throw new ArgumentException ("The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.");
233 value.Container = this;
235 metaParameters.Add (value.MetaParameter);
240 [EditorBrowsable (EditorBrowsableState.Never)]
241 [Obsolete ("Do not call this method.")]
243 public SqlParameter Add (string parameterName, object value)
245 return Add (new SqlParameter (parameterName, value));
249 public SqlParameter AddWithValue (string parameterName, object value)
251 return Add (new SqlParameter (parameterName, value));
255 public SqlParameter Add (string parameterName, SqlDbType sqlDbType)
257 return Add (new SqlParameter (parameterName, sqlDbType));
260 public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size)
262 return Add (new SqlParameter (parameterName, sqlDbType, size));
265 public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
267 return Add (new SqlParameter (parameterName, sqlDbType, size, sourceColumn));
276 metaParameters.Clear ();
278 foreach (SqlParameter p in list)
288 bool Contains (object value)
290 if (!(value is SqlParameter))
291 throw new InvalidCastException ("The parameter was not an SqlParameter.");
292 return Contains (((SqlParameter) value).ParameterName);
299 bool Contains (string value)
301 foreach (SqlParameter p in list)
302 if (p.ParameterName.Equals (value))
308 public bool Contains (SqlParameter value)
310 return (this.IndexOf(value) != -1);
318 void CopyTo (Array array, int index)
320 list.CopyTo (array, index);
327 IEnumerator GetEnumerator()
329 return list.GetEnumerator ();
336 int IndexOf (object value)
338 if (!(value is SqlParameter))
339 throw new InvalidCastException ("The parameter was not an SqlParameter.");
340 return IndexOf (((SqlParameter) value).ParameterName);
347 int IndexOf (string parameterName)
349 for (int i = 0; i < Count; i += 1)
350 if (this [i].ParameterName.Equals (parameterName))
356 public int IndexOf (SqlParameter value)
358 return list.IndexOf(value);
366 void Insert (int index, object value)
368 list.Insert (index, value);
372 public void Insert (int index, SqlParameter value)
374 list.Insert (index,value);
382 void Remove (object value)
384 //TODO : this neds validation to check if the object is a
387 ((SqlParameter) value).Container = null;
389 metaParameters.Remove (((SqlParameter) value).MetaParameter);
394 public void Remove (SqlParameter value)
396 //both this and the above code are the same. but need to work with
398 value.Container = null;
399 metaParameters.Remove (value.MetaParameter);
408 void RemoveAt (int index)
410 this [index].Container = null;
411 metaParameters.RemoveAt (index);
412 list.RemoveAt (index);
419 void RemoveAt (string parameterName)
421 RemoveAt (IndexOf (parameterName));
425 public override void AddRange (Array values)
428 throw new ArgumentNullException("The argument passed was null");
429 foreach (object value in values) {
430 if (!(value is SqlParameter))
431 throw new InvalidCastException ("Element in the array parameter was not an SqlParameter.");
432 SqlParameter param = (SqlParameter) value;
433 if (param.Container != null)
434 throw new ArgumentException ("An SqlParameter specified in the array is already added to this or another SqlParameterCollection.");
435 param.Container = this;
437 metaParameters.Add (param.MetaParameter);
441 public void AddRange (SqlParameter[] values)
443 AddRange((Array) values);
446 public void CopyTo (SqlParameter[] array, int index)
448 list.CopyTo (array, index);
452 #endregion // Methods