SqlParameterCollection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 Mono.Data.Tds;
  37. using System;
  38. using System.ComponentModel;
  39. using System.Data;
  40. using System.Data.Common;
  41. using System.Collections;
  42. namespace System.Data.SqlClient {
  43. [ListBindable (false)]
  44. [Editor ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, " + Consts.AssemblyMicrosoft_VSDesigner,
  45. "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  46. #if NET_2_0
  47. public sealed class SqlParameterCollection : DbParameterCollection, IDataParameterCollection, IList, ICollection, IEnumerable
  48. #else
  49. public sealed class SqlParameterCollection : MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
  50. #endif // NET_2_0
  51. {
  52. #region Fields
  53. ArrayList list = new ArrayList();
  54. TdsMetaParameterCollection metaParameters;
  55. SqlCommand command;
  56. #endregion // Fields
  57. #region Constructors
  58. internal SqlParameterCollection (SqlCommand command)
  59. {
  60. this.command = command;
  61. metaParameters = new TdsMetaParameterCollection ();
  62. }
  63. #endregion // Constructors
  64. #region Properties
  65. #if ONLY_1_1 || ONLY_1_0
  66. [Browsable (false)]
  67. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  68. #endif
  69. public
  70. #if NET_2_0
  71. override
  72. #endif // NET_2_0
  73. int Count {
  74. get { return list.Count; }
  75. }
  76. #if NET_2_0
  77. public override bool IsFixedSize {
  78. get {
  79. return list.IsFixedSize;
  80. }
  81. }
  82. public override bool IsReadOnly {
  83. get {
  84. return list.IsReadOnly;
  85. }
  86. }
  87. public override bool IsSynchronized {
  88. get {
  89. return list.IsSynchronized;
  90. }
  91. }
  92. public override object SyncRoot {
  93. get {
  94. return list.SyncRoot;
  95. }
  96. }
  97. #endif
  98. [Browsable (false)]
  99. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  100. public
  101. #if NET_2_0
  102. new
  103. #endif // NET_2_0
  104. SqlParameter this [int index] {
  105. get { return (SqlParameter) list [index]; }
  106. set { list [index] = (SqlParameter) value; }
  107. }
  108. object IDataParameterCollection.this [string parameterName] {
  109. get { return this[parameterName]; }
  110. set {
  111. if (!(value is SqlParameter))
  112. throw new InvalidCastException ("Only SQLParameter objects can be used.");
  113. this [parameterName] = (SqlParameter) value;
  114. }
  115. }
  116. [Browsable (false)]
  117. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  118. public
  119. #if NET_2_0
  120. new
  121. #endif // NET_2_0
  122. SqlParameter this [string parameterName] {
  123. get {
  124. foreach (SqlParameter p in list)
  125. if (p.ParameterName.Equals (parameterName))
  126. return p;
  127. throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
  128. }
  129. set {
  130. if (!Contains (parameterName))
  131. throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
  132. this [IndexOf (parameterName)] = value;
  133. }
  134. }
  135. #if NET_2_0
  136. protected override DbParameter GetParameter (int index)
  137. {
  138. return this [index];
  139. }
  140. protected override DbParameter GetParameter (string parameterName)
  141. {
  142. return this [parameterName];
  143. }
  144. protected override void SetParameter (int index, DbParameter value)
  145. {
  146. this [index] = (SqlParameter) value;
  147. }
  148. protected override void SetParameter (string parameterName, DbParameter value)
  149. {
  150. this [parameterName] = (SqlParameter) value;
  151. }
  152. #endif
  153. object IList.this [int index] {
  154. get { return (SqlParameter) this [index]; }
  155. set { this [index] = (SqlParameter) value; }
  156. }
  157. bool IList.IsFixedSize {
  158. get { return list.IsFixedSize; }
  159. }
  160. bool IList.IsReadOnly {
  161. get { return list.IsReadOnly; }
  162. }
  163. bool ICollection.IsSynchronized {
  164. get { return list.IsSynchronized; }
  165. }
  166. object ICollection.SyncRoot {
  167. get { return list.SyncRoot; }
  168. }
  169. internal TdsMetaParameterCollection MetaParameters {
  170. get { return metaParameters; }
  171. }
  172. #endregion // Properties
  173. #region Methods
  174. #if NET_2_0
  175. [EditorBrowsable (EditorBrowsableState.Never)]
  176. #endif // NET_2_0
  177. public
  178. #if NET_2_0
  179. override
  180. #endif // NET_2_0
  181. int Add (object value)
  182. {
  183. if (!(value is SqlParameter))
  184. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  185. Add ((SqlParameter) value);
  186. return IndexOf (value);
  187. }
  188. public SqlParameter Add (SqlParameter value)
  189. {
  190. if (value.Container != null)
  191. throw new ArgumentException ("The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.");
  192. value.Container = this;
  193. list.Add (value);
  194. metaParameters.Add (value.MetaParameter);
  195. return value;
  196. }
  197. #if NET_2_0
  198. [EditorBrowsable (EditorBrowsableState.Never)]
  199. [Obsolete ("Do not call this method.")]
  200. #endif // NET_2_0
  201. public SqlParameter Add (string parameterName, object value)
  202. {
  203. return Add (new SqlParameter (parameterName, value));
  204. }
  205. #if NET_2_0
  206. public SqlParameter AddWithValue (string parameterName, object value)
  207. {
  208. return Add (new SqlParameter (parameterName, value));
  209. }
  210. #endif // NET_2_0
  211. public SqlParameter Add (string parameterName, SqlDbType sqlDbType)
  212. {
  213. return Add (new SqlParameter (parameterName, sqlDbType));
  214. }
  215. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size)
  216. {
  217. return Add (new SqlParameter (parameterName, sqlDbType, size));
  218. }
  219. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
  220. {
  221. return Add (new SqlParameter (parameterName, sqlDbType, size, sourceColumn));
  222. }
  223. public
  224. #if NET_2_0
  225. override
  226. #endif // NET_2_0
  227. void Clear()
  228. {
  229. metaParameters.Clear ();
  230. foreach (SqlParameter p in list)
  231. p.Container = null;
  232. list.Clear ();
  233. }
  234. public
  235. #if NET_2_0
  236. override
  237. #endif // NET_2_0
  238. bool Contains (object value)
  239. {
  240. if (!(value is SqlParameter))
  241. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  242. return Contains (((SqlParameter) value).ParameterName);
  243. }
  244. public
  245. #if NET_2_0
  246. override
  247. #endif // NET_2_0
  248. bool Contains (string value)
  249. {
  250. foreach (SqlParameter p in list)
  251. if (p.ParameterName.Equals (value))
  252. return true;
  253. return false;
  254. }
  255. #if NET_2_0
  256. public bool Contains (SqlParameter value) {
  257. return (this.IndexOf(value) != -1);
  258. }
  259. #endif // NET_2_0
  260. public
  261. #if NET_2_0
  262. override
  263. #endif // NET_2_0
  264. void CopyTo (Array array, int index)
  265. {
  266. list.CopyTo (array, index);
  267. }
  268. public
  269. #if NET_2_0
  270. override
  271. #endif // NET_2_0
  272. IEnumerator GetEnumerator()
  273. {
  274. return list.GetEnumerator ();
  275. }
  276. public
  277. #if NET_2_0
  278. override
  279. #endif // NET_2_0
  280. int IndexOf (object value)
  281. {
  282. if (!(value is SqlParameter))
  283. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  284. return IndexOf (((SqlParameter) value).ParameterName);
  285. }
  286. public
  287. #if NET_2_0
  288. override
  289. #endif // NET_2_0
  290. int IndexOf (string parameterName)
  291. {
  292. for (int i = 0; i < Count; i += 1)
  293. if (this [i].ParameterName.Equals (parameterName))
  294. return i;
  295. return -1;
  296. }
  297. #if NET_2_0
  298. public int IndexOf (SqlParameter value) {
  299. return list.IndexOf(value);
  300. }
  301. #endif // NET_2_0
  302. public
  303. #if NET_2_0
  304. override
  305. #endif // NET_2_0
  306. void Insert (int index, object value)
  307. {
  308. list.Insert (index, value);
  309. }
  310. #if NET_2_0
  311. public void Insert (int index, SqlParameter value) {
  312. list.Insert (index,value);
  313. }
  314. #endif //NET_2_0
  315. public
  316. #if NET_2_0
  317. override
  318. #endif // NET_2_0
  319. void Remove (object value)
  320. {
  321. //TODO : this neds validation to check if the object is a
  322. // sqlparameter.
  323. ((SqlParameter) value).Container = null;
  324. metaParameters.Remove (((SqlParameter) value).MetaParameter);
  325. list.Remove (value);
  326. }
  327. #if NET_2_0
  328. public void Remove (SqlParameter value) {
  329. //both this and the above code are the same. but need to work with
  330. // 1.1!
  331. value.Container = null;
  332. metaParameters.Remove (value.MetaParameter);
  333. list.Remove (value);
  334. }
  335. #endif //NET_2_0
  336. public
  337. #if NET_2_0
  338. override
  339. #endif // NET_2_0
  340. void RemoveAt (int index)
  341. {
  342. this [index].Container = null;
  343. metaParameters.RemoveAt (index);
  344. list.RemoveAt (index);
  345. }
  346. public
  347. #if NET_2_0
  348. override
  349. #endif // NET_2_0
  350. void RemoveAt (string parameterName)
  351. {
  352. RemoveAt (IndexOf (parameterName));
  353. }
  354. #if NET_2_0
  355. public override void AddRange (Array values) {
  356. if (values == null)
  357. throw new ArgumentNullException("The argument passed was null");
  358. foreach ( object value in values) {
  359. if (!(value is SqlParameter))
  360. throw new InvalidCastException ("Element in the array parameter was not an SqlParameter.");
  361. SqlParameter param = (SqlParameter) value;
  362. if (param.Container != null)
  363. throw new ArgumentException ("An SqlParameter specified in the array is already added to this or another SqlParameterCollection.");
  364. param.Container = this;
  365. list.Add (param);
  366. metaParameters.Add (param.MetaParameter);
  367. }
  368. }
  369. public void AddRange (SqlParameter[] values) {
  370. this.AddRange(values);
  371. }
  372. public void CopyTo (SqlParameter[] array, int index)
  373. {
  374. list.CopyTo (array, index);
  375. }
  376. #endif
  377. #endregion // Methods
  378. }
  379. }