SqlParameterCollection.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // System.Data.SqlClient.SqlParameterCollection.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. // Tim Coleman ([email protected])
  8. // Diego Caravana ([email protected])
  9. //
  10. // (C) Ximian, Inc 2002
  11. // Copyright (C) Tim Coleman, 2002
  12. //
  13. //
  14. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  15. //
  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:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  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.
  34. //
  35. using Mono.Data.Tds;
  36. using System;
  37. using System.ComponentModel;
  38. using System.Data;
  39. using System.Data.Common;
  40. using System.Collections;
  41. namespace System.Data.SqlClient {
  42. [ListBindable (false)]
  43. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DataParametersEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  44. public sealed class SqlParameterCollection : MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
  45. {
  46. #region Fields
  47. ArrayList list = new ArrayList();
  48. TdsMetaParameterCollection metaParameters;
  49. SqlCommand command;
  50. #endregion // Fields
  51. #region Constructors
  52. internal SqlParameterCollection (SqlCommand command)
  53. {
  54. this.command = command;
  55. metaParameters = new TdsMetaParameterCollection ();
  56. }
  57. #endregion // Constructors
  58. #region Properties
  59. [Browsable (false)]
  60. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  61. public int Count {
  62. get { return list.Count; }
  63. }
  64. [Browsable (false)]
  65. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  66. public SqlParameter this [int index] {
  67. get { return (SqlParameter) list [index]; }
  68. set { list [index] = (SqlParameter) value; }
  69. }
  70. object IDataParameterCollection.this [string parameterName] {
  71. get { return this[parameterName]; }
  72. set {
  73. if (!(value is SqlParameter))
  74. throw new InvalidCastException ("Only SQLParameter objects can be used.");
  75. this [parameterName] = (SqlParameter) value;
  76. }
  77. }
  78. [Browsable (false)]
  79. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  80. public SqlParameter this [string parameterName] {
  81. get {
  82. foreach (SqlParameter p in list)
  83. if (p.ParameterName.Equals (parameterName))
  84. return p;
  85. throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
  86. }
  87. set {
  88. if (!Contains (parameterName))
  89. throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
  90. this [IndexOf (parameterName)] = value;
  91. }
  92. }
  93. object IList.this [int index] {
  94. get { return (SqlParameter) this [index]; }
  95. set { this [index] = (SqlParameter) value; }
  96. }
  97. bool IList.IsFixedSize {
  98. get { return list.IsFixedSize; }
  99. }
  100. bool IList.IsReadOnly {
  101. get { return list.IsReadOnly; }
  102. }
  103. bool ICollection.IsSynchronized {
  104. get { return list.IsSynchronized; }
  105. }
  106. object ICollection.SyncRoot {
  107. get { return list.SyncRoot; }
  108. }
  109. internal TdsMetaParameterCollection MetaParameters {
  110. get { return metaParameters; }
  111. }
  112. #endregion // Properties
  113. #region Methods
  114. public int Add (object value)
  115. {
  116. if (!(value is SqlParameter))
  117. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  118. Add ((SqlParameter) value);
  119. return IndexOf (value);
  120. }
  121. public SqlParameter Add (SqlParameter value)
  122. {
  123. if (value.Container != null)
  124. throw new ArgumentException ("The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.");
  125. value.Container = this;
  126. list.Add (value);
  127. metaParameters.Add (value.MetaParameter);
  128. return value;
  129. }
  130. public SqlParameter Add (string parameterName, object value)
  131. {
  132. return Add (new SqlParameter (parameterName, value));
  133. }
  134. public SqlParameter Add (string parameterName, SqlDbType sqlDbType)
  135. {
  136. return Add (new SqlParameter (parameterName, sqlDbType));
  137. }
  138. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size)
  139. {
  140. return Add (new SqlParameter (parameterName, sqlDbType, size));
  141. }
  142. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
  143. {
  144. return Add (new SqlParameter (parameterName, sqlDbType, size, sourceColumn));
  145. }
  146. public void Clear()
  147. {
  148. metaParameters.Clear ();
  149. foreach (SqlParameter p in list)
  150. p.Container = null;
  151. list.Clear ();
  152. }
  153. public bool Contains (object value)
  154. {
  155. if (!(value is SqlParameter))
  156. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  157. return Contains (((SqlParameter) value).ParameterName);
  158. }
  159. public bool Contains (string value)
  160. {
  161. foreach (SqlParameter p in list)
  162. if (p.ParameterName.Equals (value))
  163. return true;
  164. return false;
  165. }
  166. public void CopyTo (Array array, int index)
  167. {
  168. list.CopyTo (array, index);
  169. }
  170. public IEnumerator GetEnumerator()
  171. {
  172. return list.GetEnumerator ();
  173. }
  174. public int IndexOf (object value)
  175. {
  176. if (!(value is SqlParameter))
  177. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  178. return IndexOf (((SqlParameter) value).ParameterName);
  179. }
  180. public int IndexOf (string parameterName)
  181. {
  182. for (int i = 0; i < Count; i += 1)
  183. if (this [i].ParameterName.Equals (parameterName))
  184. return i;
  185. return -1;
  186. }
  187. public void Insert (int index, object value)
  188. {
  189. list.Insert (index, value);
  190. }
  191. public void Remove (object value)
  192. {
  193. ((SqlParameter) value).Container = null;
  194. metaParameters.Remove (((SqlParameter) value).MetaParameter);
  195. list.Remove (value);
  196. }
  197. public void RemoveAt (int index)
  198. {
  199. this [index].Container = null;
  200. metaParameters.RemoveAt (index);
  201. list.RemoveAt (index);
  202. }
  203. public void RemoveAt (string parameterName)
  204. {
  205. RemoveAt (IndexOf (parameterName));
  206. }
  207. #endregion // Methods
  208. }
  209. }