1 // ByteFX.Data data access components for .Net
2 // Copyright (C) 2002-2003 ByteFX, Inc.
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 using System
.Collections
;
21 using System
.ComponentModel
;
23 namespace ByteFX
.Data
.MySqlClient
26 /// Represents a collection of parameters relevant to a <see cref="MySqlCommand"/> as well as their respective mappings to columns in a <see cref="DataSet"/>. This class cannot be inherited.
28 /// <include file='docs/MySqlParameterCollection.xml' path='MyDocs/MyMembers[@name="Class"]/*'/>
29 [Editor(typeof(ByteFX
.Data
.Common
.DBParametersEditor
), typeof(System
.Drawing
.Design
.UITypeEditor
))]
31 public sealed class MySqlParameterCollection
: MarshalByRefObject
, IDataParameterCollection
,
32 IList
, ICollection
, IEnumerable
34 private ArrayList _parms
= new ArrayList();
36 #region ICollection support
39 /// Gets the number of MySqlParameter objects in the collection.
43 get { return _parms.Count; }
47 /// Copies MySqlParameter objects from the MySqlParameterCollection to the specified array.
49 /// <param name="array"></param>
50 /// <param name="index"></param>
51 public void CopyTo( Array array
, int index
)
53 _parms
.CopyTo(array
, index
);
56 bool ICollection
.IsSynchronized
58 get { return _parms.IsSynchronized; }
61 object ICollection
.SyncRoot
63 get { return _parms.SyncRoot; }
70 /// Removes all items from the collection.
78 /// Gets a value indicating whether a MySqlParameter exists in the collection.
80 /// <param name="value">The value of the <see cref="MySqlParameter"/> object to find. </param>
81 /// <returns>true if the collection contains the <see cref="MySqlParameter"/> object; otherwise, false.</returns>
82 /// <overloads>Gets a value indicating whether a <see cref="MySqlParameter"/> exists in the collection.</overloads>
83 public bool Contains(object value)
85 return _parms
.Contains(value);
89 /// Gets the location of a <see cref="MySqlParameter"/> in the collection.
91 /// <param name="value">The <see cref="MySqlParameter"/> object to locate. </param>
92 /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
93 /// <overloads>Gets the location of a <see cref="MySqlParameter"/> in the collection.</overloads>
94 public int IndexOf(object value)
96 return _parms
.IndexOf(value);
100 /// Inserts a MySqlParameter into the collection at the specified index.
102 /// <param name="index"></param>
103 /// <param name="value"></param>
104 public void Insert(int index
, object value)
106 _parms
.Insert( index
, value );
109 bool IList
.IsFixedSize
111 get { return _parms.IsFixedSize; }
114 bool IList
.IsReadOnly
116 get { return _parms.IsReadOnly; }
120 /// Removes the specified MySqlParameter from the collection.
122 /// <param name="value"></param>
123 public void Remove( object value )
125 _parms
.Remove( value );
129 /// Removes the specified <see cref="MySqlParameter"/> from the collection using a specific index.
131 /// <param name="index">The zero-based index of the parameter. </param>
132 /// <overloads>Removes the specified <see cref="MySqlParameter"/> from the collection.</overloads>
133 public void RemoveAt( int index
)
135 _parms
.RemoveAt( index
);
138 object IList
.this[int index
]
140 get { return this[index]; }
143 if (! (value is MySqlParameter
)) throw new MySqlException("Only MySqlParameter objects may be stored");
144 this[index
] = (MySqlParameter
)value;
149 /// Adds the specified <see cref="MySqlParameter"/> object to the <see cref="MySqlParameterCollection"/>.
151 /// <param name="value">The <see cref="MySqlParameter"/> to add to the collection.</param>
152 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
153 public int Add( object value )
155 if (! (value is MySqlParameter
)) throw new MySqlException("Only MySqlParameter objects may be stored");
157 MySqlParameter p
= (MySqlParameter
)value;
159 if (p
.ParameterName
== null || p
.ParameterName
== String
.Empty
)
160 throw new MySqlException("Parameters must be named");
162 return _parms
.Add(value);
167 #region IDataParameterCollection
170 /// Gets a value indicating whether a <see cref="MySqlParameter"/> with the specified parameter name exists in the collection.
172 /// <param name="name">The name of the <see cref="MySqlParameter"/> object to find.</param>
173 /// <returns>true if the collection contains the parameter; otherwise, false.</returns>
174 public bool Contains(string name
)
177 name
= name
.Substring(1, name
.Length
-1);
178 foreach (MySqlParameter p
in _parms
)
180 if (p
.ParameterName
.ToLower().Equals( name
.ToLower() )) return true;
186 /// Gets the location of the <see cref="MySqlParameter"/> in the collection with a specific parameter name.
188 /// <param name="parameterName">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
189 /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
190 public int IndexOf( string parameterName
)
192 if (parameterName
[0] == '@')
193 parameterName
= parameterName
.Substring(1, parameterName
.Length
-1);
194 for (int x
=0; x
< _parms
.Count
; x
++)
196 MySqlParameter p
= (MySqlParameter
)_parms
[x
];
197 if (p
.ParameterName
.ToLower().Equals( parameterName
.ToLower() )) return x
;
199 throw new MySqlException("Parameter '" + parameterName
+ "' not found in collection");
203 /// Removes the specified <see cref="MySqlParameter"/> from the collection using the parameter name.
205 /// <param name="name">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
206 public void RemoveAt( string name
)
208 int index
= IndexOf( name
);
209 _parms
.RemoveAt(index
);
212 object IDataParameterCollection
.this[string name
]
214 get { return this[name]; }
217 if (! (value is MySqlParameter
)) throw new MySqlException("Only MySqlParameter objects may be stored");
218 this[name
] = (MySqlParameter
)value;
224 IEnumerator IEnumerable
.GetEnumerator()
226 return ((IEnumerable
)_parms
).GetEnumerator();
230 #region Public Methods
233 /// Gets the <see cref="MySqlParameter"/> at the specified index.
235 /// <overloads>Gets the <see cref="MySqlParameter"/> with a specified attribute.
236 /// [C#] In C#, this property is the indexer for the <see cref="MySqlParameterCollection"/> class.
238 public MySqlParameter
this[int index
]
240 get { return (MySqlParameter)_parms[index]; }
241 set { _parms[index] = value; }
245 /// Gets the <see cref="MySqlParameter"/> with the specified name.
247 public MySqlParameter
this[string name
]
249 get { return (MySqlParameter)_parms[ IndexOf( name ) ]; }
250 set { _parms[ IndexOf( name ) ] = value; }
254 /// Adds the specified <see cref="MySqlParameter"/> object to the <see cref="MySqlParameterCollection"/>.
256 /// <param name="value">The <see cref="MySqlParameter"/> to add to the collection.</param>
257 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
258 public MySqlParameter
Add(MySqlParameter
value)
260 if ( value.ParameterName
== null ) throw new ArgumentException("parameter must be named");
267 /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the specified parameter name and value.
269 /// <param name="parameterName">The name of the parameter.</param>
270 /// <param name="value">The <see cref="MySqlParameter.Value"/> of the <see cref="MySqlParameter"/> to add to the collection.</param>
271 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
272 public MySqlParameter
Add( string parameterName
, object value )
274 return Add( new MySqlParameter( parameterName
, value ) );
278 /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the parameter name and the data type.
280 /// <param name="parameterName">The name of the parameter.</param>
281 /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
282 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
283 public MySqlParameter
Add(string parameterName
, MySqlDbType dbType
)
285 return Add(new MySqlParameter(parameterName
, dbType
));
289 /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> with the parameter name, the data type, and the column length.
291 /// <param name="parameterName">The name of the parameter.</param>
292 /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
293 /// <param name="size">The length of the column.</param>
294 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
295 public MySqlParameter
Add(string parameterName
, MySqlDbType dbType
, int size
)
297 return Add(new MySqlParameter(parameterName
, dbType
, size
));
301 /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> with the parameter name, the data type, the column length, and the source column name.
303 /// <param name="parameterName">The name of the parameter.</param>
304 /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
305 /// <param name="size">The length of the column.</param>
306 /// <param name="sourceColumn">The name of the source column.</param>
307 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
308 public MySqlParameter
Add(string parameterName
, MySqlDbType dbType
, int size
, string sourceColumn
)
310 return Add(new MySqlParameter(parameterName
, dbType
, size
, sourceColumn
));