SqlParameterCollection.cs 11 KB

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