SqlParameterCollection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 override DbParameter GetParameter (int index)
  140. {
  141. throw new NotImplementedException();
  142. }
  143. protected override DbParameter GetParameter (string parameterName)
  144. {
  145. throw new NotImplementedException ();
  146. }
  147. protected override void SetParameter (int index, DbParameter value)
  148. {
  149. throw new NotImplementedException();
  150. }
  151. protected override void SetParameter (string parameterName, DbParameter value)
  152. {
  153. throw new NotImplementedException();
  154. }
  155. /*public IEnumerator GetEnumerator ()
  156. /*public IEnumerator GetEnumerator ()
  157. {
  158. throw new NotImplementedException();
  159. }*/
  160. protected override Type ItemType
  161. {
  162. get {
  163. throw new NotImplementedException();
  164. }
  165. }
  166. #endif
  167. object IList.this [int index] {
  168. get { return (SqlParameter) this [index]; }
  169. set { this [index] = (SqlParameter) value; }
  170. }
  171. bool IList.IsFixedSize {
  172. get { return list.IsFixedSize; }
  173. }
  174. bool IList.IsReadOnly {
  175. get { return list.IsReadOnly; }
  176. }
  177. bool ICollection.IsSynchronized {
  178. get { return list.IsSynchronized; }
  179. }
  180. object ICollection.SyncRoot {
  181. get { return list.SyncRoot; }
  182. }
  183. internal TdsMetaParameterCollection MetaParameters {
  184. get { return metaParameters; }
  185. }
  186. #endregion // Properties
  187. #region Methods
  188. public
  189. #if NET_2_0
  190. override
  191. #endif // NET_2_0
  192. int Add (object value)
  193. {
  194. if (!(value is SqlParameter))
  195. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  196. Add ((SqlParameter) value);
  197. return IndexOf (value);
  198. }
  199. public SqlParameter Add (SqlParameter value)
  200. {
  201. if (value.Container != null)
  202. throw new ArgumentException ("The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.");
  203. value.Container = this;
  204. list.Add (value);
  205. metaParameters.Add (value.MetaParameter);
  206. return value;
  207. }
  208. public SqlParameter Add (string parameterName, object value)
  209. {
  210. return Add (new SqlParameter (parameterName, value));
  211. }
  212. #if NET_2_0
  213. public SqlParameter AddWithValue (string parameterName, object value)
  214. {
  215. return Add (parameterName, value);
  216. }
  217. #endif // NET_2_0
  218. public SqlParameter Add (string parameterName, SqlDbType sqlDbType)
  219. {
  220. return Add (new SqlParameter (parameterName, sqlDbType));
  221. }
  222. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size)
  223. {
  224. return Add (new SqlParameter (parameterName, sqlDbType, size));
  225. }
  226. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
  227. {
  228. return Add (new SqlParameter (parameterName, sqlDbType, size, sourceColumn));
  229. }
  230. public
  231. #if NET_2_0
  232. override
  233. #endif // NET_2_0
  234. void Clear()
  235. {
  236. metaParameters.Clear ();
  237. foreach (SqlParameter p in list)
  238. p.Container = null;
  239. list.Clear ();
  240. }
  241. public
  242. #if NET_2_0
  243. override
  244. #endif // NET_2_0
  245. bool Contains (object value)
  246. {
  247. if (!(value is SqlParameter))
  248. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  249. return Contains (((SqlParameter) value).ParameterName);
  250. }
  251. public
  252. #if NET_2_0
  253. override
  254. #endif // NET_2_0
  255. bool Contains (string value)
  256. {
  257. foreach (SqlParameter p in list)
  258. if (p.ParameterName.Equals (value))
  259. return true;
  260. return false;
  261. }
  262. #if NET_2_0
  263. public bool Contains (SqlParameter value) {
  264. return (this.IndexOf(value) != -1);
  265. }
  266. #endif // NET_2_0
  267. public
  268. #if NET_2_0
  269. override
  270. #endif // NET_2_0
  271. void CopyTo (Array array, int index)
  272. {
  273. list.CopyTo (array, index);
  274. }
  275. public
  276. #if NET_2_0
  277. override
  278. #endif // NET_2_0
  279. IEnumerator GetEnumerator()
  280. {
  281. return list.GetEnumerator ();
  282. }
  283. public
  284. #if NET_2_0
  285. override
  286. #endif // NET_2_0
  287. int IndexOf (object value)
  288. {
  289. if (!(value is SqlParameter))
  290. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  291. return IndexOf (((SqlParameter) value).ParameterName);
  292. }
  293. public
  294. #if NET_2_0
  295. override
  296. #endif // NET_2_0
  297. int IndexOf (string parameterName)
  298. {
  299. for (int i = 0; i < Count; i += 1)
  300. if (this [i].ParameterName.Equals (parameterName))
  301. return i;
  302. return -1;
  303. }
  304. #if NET_2_0
  305. public int IndexOf (SqlParameter value) {
  306. return list.IndexOf(value);
  307. }
  308. #endif // NET_2_0
  309. public
  310. #if NET_2_0
  311. override
  312. #endif // NET_2_0
  313. void Insert (int index, object value)
  314. {
  315. list.Insert (index, value);
  316. }
  317. #if NET_2_0
  318. public void Insert (int index, SqlParameter value) {
  319. list.Insert (index,value);
  320. }
  321. #endif //NET_2_0
  322. public
  323. #if NET_2_0
  324. override
  325. #endif // NET_2_0
  326. void Remove (object value)
  327. {
  328. //TODO : this neds validation to check if the object is a
  329. // sqlparameter.
  330. ((SqlParameter) value).Container = null;
  331. metaParameters.Remove (((SqlParameter) value).MetaParameter);
  332. list.Remove (value);
  333. }
  334. #if NET_2_0
  335. public void Remove (SqlParameter value) {
  336. //both this and the above code are the same. but need to work with
  337. // 1.1!
  338. value.Container = null;
  339. metaParameters.Remove (value.MetaParameter);
  340. list.Remove (value);
  341. }
  342. #endif //NET_2_0
  343. public
  344. #if NET_2_0
  345. override
  346. #endif // NET_2_0
  347. void RemoveAt (int index)
  348. {
  349. this [index].Container = null;
  350. metaParameters.RemoveAt (index);
  351. list.RemoveAt (index);
  352. }
  353. public
  354. #if NET_2_0
  355. override
  356. #endif // NET_2_0
  357. void RemoveAt (string parameterName)
  358. {
  359. RemoveAt (IndexOf (parameterName));
  360. }
  361. #if NET_2_0
  362. public override void AddRange (Array values) {
  363. if (values == null)
  364. throw new ArgumentNullException("The argument passed was null");
  365. foreach ( object value in values) {
  366. if (!(value is SqlParameter))
  367. throw new InvalidCastException ("Element in the array parameter was not an SqlParameter.");
  368. SqlParameter param = (SqlParameter) value;
  369. if (param.Container != null)
  370. throw new ArgumentException ("An SqlParameter specified in the array is already added to this or another SqlParameterCollection.");
  371. param.Container = this;
  372. list.Add (param);
  373. metaParameters.Add (param.MetaParameter);
  374. }
  375. }
  376. public void AddRange (SqlParameter[] values) {
  377. this.AddRange(values);
  378. }
  379. #endif
  380. #endregion // Methods
  381. }
  382. }