SqlParameterCollection.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. // Umadevi S ([email protected])
  10. //
  11. // (C) Ximian, Inc 2002
  12. // Copyright (C) Tim Coleman, 2002
  13. //
  14. //
  15. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  16. //
  17. // Permission is hereby granted, free of charge, to any person obtaining
  18. // a copy of this software and associated documentation files (the
  19. // "Software"), to deal in the Software without restriction, including
  20. // without limitation the rights to use, copy, modify, merge, publish,
  21. // distribute, sublicense, and/or sell copies of the Software, and to
  22. // permit persons to whom the Software is furnished to do so, subject to
  23. // the following conditions:
  24. //
  25. // The above copyright notice and this permission notice shall be
  26. // included in all copies or substantial portions of the Software.
  27. //
  28. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  32. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  33. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. //
  36. using System;
  37. using System.Collections;
  38. using System.ComponentModel;
  39. using System.Data;
  40. using System.Data.Common;
  41. using Mono.Data.Tds;
  42. namespace System.Data.SqlClient
  43. {
  44. [ListBindable (false)]
  45. [Editor ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, " + Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  46. public sealed class SqlParameterCollection : DbParameterCollection, IDataParameterCollection, IList, ICollection, IEnumerable
  47. {
  48. #region Fields
  49. ArrayList list = new ArrayList();
  50. TdsMetaParameterCollection metaParameters;
  51. SqlCommand command;
  52. #endregion // Fields
  53. #region Constructors
  54. internal SqlParameterCollection (SqlCommand command)
  55. {
  56. this.command = command;
  57. metaParameters = new TdsMetaParameterCollection ();
  58. }
  59. #endregion // Constructors
  60. #region Properties
  61. public
  62. override
  63. int Count {
  64. get { return list.Count; }
  65. }
  66. public override bool IsFixedSize {
  67. get {
  68. return list.IsFixedSize;
  69. }
  70. }
  71. public override bool IsReadOnly {
  72. get {
  73. return list.IsReadOnly;
  74. }
  75. }
  76. public override bool IsSynchronized {
  77. get {
  78. return list.IsSynchronized;
  79. }
  80. }
  81. public override object SyncRoot {
  82. get {
  83. return list.SyncRoot;
  84. }
  85. }
  86. [Browsable (false)]
  87. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  88. public
  89. new
  90. SqlParameter this [int index] {
  91. get {
  92. if (index < 0 || index >= list.Count)
  93. throw new IndexOutOfRangeException ("The specified index is out of range.");
  94. return (SqlParameter) list [index];
  95. }
  96. set {
  97. if (index < 0 || index >= list.Count)
  98. throw new IndexOutOfRangeException ("The specified index is out of range.");
  99. list [index] = (SqlParameter) value;
  100. }
  101. }
  102. [Browsable (false)]
  103. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  104. public
  105. new
  106. SqlParameter this [string parameterName] {
  107. get {
  108. foreach (SqlParameter p in list)
  109. if (p.ParameterName.Equals (parameterName))
  110. return p;
  111. throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
  112. }
  113. set {
  114. if (!Contains (parameterName))
  115. throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
  116. this [IndexOf (parameterName)] = value;
  117. }
  118. }
  119. protected override DbParameter GetParameter (int index)
  120. {
  121. return this [index];
  122. }
  123. protected override DbParameter GetParameter (string parameterName)
  124. {
  125. return this [parameterName];
  126. }
  127. protected override void SetParameter (int index, DbParameter value)
  128. {
  129. this [index] = (SqlParameter) value;
  130. }
  131. protected override void SetParameter (string parameterName, DbParameter value)
  132. {
  133. this [parameterName] = (SqlParameter) value;
  134. }
  135. internal TdsMetaParameterCollection MetaParameters {
  136. get { return metaParameters; }
  137. }
  138. #endregion // Properties
  139. #region Methods
  140. [EditorBrowsable (EditorBrowsableState.Never)]
  141. public
  142. override
  143. int Add (object value)
  144. {
  145. if (!(value is SqlParameter))
  146. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  147. Add ((SqlParameter) value);
  148. return IndexOf (value);
  149. }
  150. public SqlParameter Add (SqlParameter value)
  151. {
  152. if (value.Container != null)
  153. throw new ArgumentException ("The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.");
  154. value.Container = this;
  155. list.Add (value);
  156. metaParameters.Add (value.MetaParameter);
  157. return value;
  158. }
  159. [EditorBrowsable (EditorBrowsableState.Never)]
  160. [Obsolete ("Do not call this method.")]
  161. public SqlParameter Add (string parameterName, object value)
  162. {
  163. return Add (new SqlParameter (parameterName, value));
  164. }
  165. public SqlParameter AddWithValue (string parameterName, object value)
  166. {
  167. return Add (new SqlParameter (parameterName, value));
  168. }
  169. public SqlParameter Add (string parameterName, SqlDbType sqlDbType)
  170. {
  171. return Add (new SqlParameter (parameterName, sqlDbType));
  172. }
  173. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size)
  174. {
  175. return Add (new SqlParameter (parameterName, sqlDbType, size));
  176. }
  177. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
  178. {
  179. return Add (new SqlParameter (parameterName, sqlDbType, size, sourceColumn));
  180. }
  181. public
  182. override
  183. void Clear()
  184. {
  185. metaParameters.Clear ();
  186. foreach (SqlParameter p in list)
  187. p.Container = null;
  188. list.Clear ();
  189. }
  190. public
  191. override
  192. bool Contains (object value)
  193. {
  194. if (!(value is SqlParameter))
  195. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  196. return Contains (((SqlParameter) value).ParameterName);
  197. }
  198. public
  199. override
  200. bool Contains (string value)
  201. {
  202. foreach (SqlParameter p in list)
  203. if (p.ParameterName.Equals (value))
  204. return true;
  205. return false;
  206. }
  207. public bool Contains (SqlParameter value)
  208. {
  209. return (this.IndexOf(value) != -1);
  210. }
  211. public
  212. override
  213. void CopyTo (Array array, int index)
  214. {
  215. list.CopyTo (array, index);
  216. }
  217. public
  218. override
  219. IEnumerator GetEnumerator()
  220. {
  221. return list.GetEnumerator ();
  222. }
  223. public
  224. override
  225. int IndexOf (object value)
  226. {
  227. if (!(value is SqlParameter))
  228. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  229. return IndexOf (((SqlParameter) value).ParameterName);
  230. }
  231. public
  232. override
  233. int IndexOf (string parameterName)
  234. {
  235. for (int i = 0; i < Count; i += 1)
  236. if (this [i].ParameterName.Equals (parameterName))
  237. return i;
  238. return -1;
  239. }
  240. public int IndexOf (SqlParameter value)
  241. {
  242. return list.IndexOf(value);
  243. }
  244. public
  245. override
  246. void Insert (int index, object value)
  247. {
  248. list.Insert (index, value);
  249. }
  250. public void Insert (int index, SqlParameter value)
  251. {
  252. list.Insert (index,value);
  253. }
  254. public
  255. override
  256. void Remove (object value)
  257. {
  258. //TODO : this neds validation to check if the object is a
  259. // sqlparameter.
  260. ((SqlParameter) value).Container = null;
  261. metaParameters.Remove (((SqlParameter) value).MetaParameter);
  262. list.Remove (value);
  263. }
  264. public void Remove (SqlParameter value)
  265. {
  266. //both this and the above code are the same. but need to work with
  267. // 1.1!
  268. value.Container = null;
  269. metaParameters.Remove (value.MetaParameter);
  270. list.Remove (value);
  271. }
  272. public
  273. override
  274. void RemoveAt (int index)
  275. {
  276. this [index].Container = null;
  277. metaParameters.RemoveAt (index);
  278. list.RemoveAt (index);
  279. }
  280. public
  281. override
  282. void RemoveAt (string parameterName)
  283. {
  284. RemoveAt (IndexOf (parameterName));
  285. }
  286. public override void AddRange (Array values)
  287. {
  288. if (values == null)
  289. throw new ArgumentNullException("The argument passed was null");
  290. foreach (object value in values) {
  291. if (!(value is SqlParameter))
  292. throw new InvalidCastException ("Element in the array parameter was not an SqlParameter.");
  293. SqlParameter param = (SqlParameter) value;
  294. if (param.Container != null)
  295. throw new ArgumentException ("An SqlParameter specified in the array is already added to this or another SqlParameterCollection.");
  296. param.Container = this;
  297. list.Add (param);
  298. metaParameters.Add (param.MetaParameter);
  299. }
  300. }
  301. public void AddRange (SqlParameter[] values)
  302. {
  303. AddRange((Array) values);
  304. }
  305. public void CopyTo (SqlParameter[] array, int index)
  306. {
  307. list.CopyTo (array, index);
  308. }
  309. #endregion // Methods
  310. }
  311. }