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.
40 using System.ComponentModel;
42 using System.Data.Common;
43 using System.Collections;
45 namespace System.Data.SqlClient {
46 [ListBindable (false)]
47 [Editor ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, " + Consts.AssemblyMicrosoft_VSDesigner,
48 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
50 public sealed class SqlParameterCollection : DbParameterCollection, IDataParameterCollection, IList, ICollection, IEnumerable
52 public sealed class SqlParameterCollection : MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
57 ArrayList list = new ArrayList();
58 TdsMetaParameterCollection metaParameters;
65 internal SqlParameterCollection (SqlCommand command)
67 this.command = command;
68 metaParameters = new TdsMetaParameterCollection ();
71 #endregion // Constructors
74 #if ONLY_1_1 || ONLY_1_0
76 [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;
117 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
122 SqlParameter this [int index] {
123 get { return (SqlParameter) list [index]; }
124 set { list [index] = (SqlParameter) value; }
127 object IDataParameterCollection.this [string parameterName] {
128 get { return this[parameterName]; }
130 if (!(value is SqlParameter))
131 throw new InvalidCastException ("Only SQLParameter objects can be used.");
132 this [parameterName] = (SqlParameter) value;
137 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
142 SqlParameter this [string parameterName] {
144 foreach (SqlParameter p in list)
145 if (p.ParameterName.Equals (parameterName))
147 throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
150 if (!Contains (parameterName))
151 throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
152 this [IndexOf (parameterName)] = value;
157 protected override DbParameter GetParameter (int index)
162 protected override DbParameter GetParameter (string parameterName)
164 return this [parameterName];
167 protected override void SetParameter (int index, DbParameter value)
169 this [index] = (SqlParameter) value;
171 protected override void SetParameter (string parameterName, DbParameter value)
173 this [parameterName] = (SqlParameter) value;
176 object IList.this [int index] {
177 get { return (SqlParameter) this [index]; }
178 set { this [index] = (SqlParameter) value; }
181 bool IList.IsFixedSize {
182 get { return list.IsFixedSize; }
185 bool IList.IsReadOnly {
186 get { return list.IsReadOnly; }
189 bool ICollection.IsSynchronized {
190 get { return list.IsSynchronized; }
193 object ICollection.SyncRoot {
194 get { return list.SyncRoot; }
199 internal TdsMetaParameterCollection MetaParameters {
200 get { return metaParameters; }
203 #endregion // Properties
208 [EditorBrowsable (EditorBrowsableState.Never)]
214 int Add (object value)
216 if (!(value is SqlParameter))
217 throw new InvalidCastException ("The parameter was not an SqlParameter.");
218 Add ((SqlParameter) value);
219 return IndexOf (value);
222 public SqlParameter Add (SqlParameter value)
224 if (value.Container != null)
225 throw new ArgumentException ("The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.");
227 value.Container = this;
229 metaParameters.Add (value.MetaParameter);
234 [EditorBrowsable (EditorBrowsableState.Never)]
235 [Obsolete ("Do not call this method.")]
237 public SqlParameter Add (string parameterName, object value)
239 return Add (new SqlParameter (parameterName, value));
243 public SqlParameter AddWithValue (string parameterName, object value)
245 return Add (new SqlParameter (parameterName, value));
250 public SqlParameter Add (string parameterName, SqlDbType sqlDbType)
252 return Add (new SqlParameter (parameterName, sqlDbType));
255 public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size)
257 return Add (new SqlParameter (parameterName, sqlDbType, size));
260 public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
262 return Add (new SqlParameter (parameterName, sqlDbType, size, sourceColumn));
271 metaParameters.Clear ();
273 foreach (SqlParameter p in list)
283 bool Contains (object value)
285 if (!(value is SqlParameter))
286 throw new InvalidCastException ("The parameter was not an SqlParameter.");
287 return Contains (((SqlParameter) value).ParameterName);
294 bool Contains (string value)
296 foreach (SqlParameter p in list)
297 if (p.ParameterName.Equals (value))
303 public bool Contains (SqlParameter value) {
305 return (this.IndexOf(value) != -1);
315 void CopyTo (Array array, int index)
317 list.CopyTo (array, index);
324 IEnumerator GetEnumerator()
326 return list.GetEnumerator ();
333 int IndexOf (object value)
335 if (!(value is SqlParameter))
336 throw new InvalidCastException ("The parameter was not an SqlParameter.");
337 return IndexOf (((SqlParameter) value).ParameterName);
344 int IndexOf (string parameterName)
346 for (int i = 0; i < Count; i += 1)
347 if (this [i].ParameterName.Equals (parameterName))
354 public int IndexOf (SqlParameter value) {
355 return list.IndexOf(value);
364 void Insert (int index, object value)
366 list.Insert (index, value);
370 public void Insert (int index, SqlParameter value) {
371 list.Insert (index,value);
380 void Remove (object value)
382 //TODO : this neds validation to check if the object is a
385 ((SqlParameter) value).Container = null;
387 metaParameters.Remove (((SqlParameter) value).MetaParameter);
393 public void Remove (SqlParameter value) {
395 //both this and the above code are the same. but need to work with
397 value.Container = null;
398 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.");
436 param.Container = this;
438 metaParameters.Add (param.MetaParameter);
443 public void AddRange (SqlParameter[] values) {
445 this.AddRange((Array) values);
448 public void CopyTo (SqlParameter[] array, int index)
450 list.CopyTo (array, index);
454 #endregion // Methods