SqlParameterCollection.cs 11 KB

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