SqlParameterCollection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #if NET_2_0
  204. public SqlParameter AddWithValue (string parameterName, object value)
  205. {
  206. return Add (parameterName, value);
  207. }
  208. #endif // NET_2_0
  209. public SqlParameter Add (string parameterName, SqlDbType sqlDbType)
  210. {
  211. return Add (new SqlParameter (parameterName, sqlDbType));
  212. }
  213. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size)
  214. {
  215. return Add (new SqlParameter (parameterName, sqlDbType, size));
  216. }
  217. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
  218. {
  219. return Add (new SqlParameter (parameterName, sqlDbType, size, sourceColumn));
  220. }
  221. public
  222. #if NET_2_0
  223. override
  224. #endif // NET_2_0
  225. void Clear()
  226. {
  227. metaParameters.Clear ();
  228. foreach (SqlParameter p in list)
  229. p.Container = null;
  230. list.Clear ();
  231. }
  232. public
  233. #if NET_2_0
  234. override
  235. #endif // NET_2_0
  236. bool Contains (object value)
  237. {
  238. if (!(value is SqlParameter))
  239. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  240. return Contains (((SqlParameter) value).ParameterName);
  241. }
  242. public
  243. #if NET_2_0
  244. override
  245. #endif // NET_2_0
  246. bool Contains (string value)
  247. {
  248. foreach (SqlParameter p in list)
  249. if (p.ParameterName.Equals (value))
  250. return true;
  251. return false;
  252. }
  253. #if NET_2_0
  254. public bool Contains (SqlParameter value) {
  255. return (this.IndexOf(value) != -1);
  256. }
  257. #endif // NET_2_0
  258. public
  259. #if NET_2_0
  260. override
  261. #endif // NET_2_0
  262. void CopyTo (Array array, int index)
  263. {
  264. list.CopyTo (array, index);
  265. }
  266. public
  267. #if NET_2_0
  268. override
  269. #endif // NET_2_0
  270. IEnumerator GetEnumerator()
  271. {
  272. return list.GetEnumerator ();
  273. }
  274. public
  275. #if NET_2_0
  276. override
  277. #endif // NET_2_0
  278. int IndexOf (object value)
  279. {
  280. if (!(value is SqlParameter))
  281. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  282. return IndexOf (((SqlParameter) value).ParameterName);
  283. }
  284. public
  285. #if NET_2_0
  286. override
  287. #endif // NET_2_0
  288. int IndexOf (string parameterName)
  289. {
  290. for (int i = 0; i < Count; i += 1)
  291. if (this [i].ParameterName.Equals (parameterName))
  292. return i;
  293. return -1;
  294. }
  295. #if NET_2_0
  296. public int IndexOf (SqlParameter value) {
  297. return list.IndexOf(value);
  298. }
  299. #endif // NET_2_0
  300. public
  301. #if NET_2_0
  302. override
  303. #endif // NET_2_0
  304. void Insert (int index, object value)
  305. {
  306. list.Insert (index, value);
  307. }
  308. #if NET_2_0
  309. public void Insert (int index, SqlParameter value) {
  310. list.Insert (index,value);
  311. }
  312. #endif //NET_2_0
  313. public
  314. #if NET_2_0
  315. override
  316. #endif // NET_2_0
  317. void Remove (object value)
  318. {
  319. //TODO : this neds validation to check if the object is a
  320. // sqlparameter.
  321. ((SqlParameter) value).Container = null;
  322. metaParameters.Remove (((SqlParameter) value).MetaParameter);
  323. list.Remove (value);
  324. }
  325. #if NET_2_0
  326. public void Remove (SqlParameter value) {
  327. //both this and the above code are the same. but need to work with
  328. // 1.1!
  329. value.Container = null;
  330. metaParameters.Remove (value.MetaParameter);
  331. list.Remove (value);
  332. }
  333. #endif //NET_2_0
  334. public
  335. #if NET_2_0
  336. override
  337. #endif // NET_2_0
  338. void RemoveAt (int index)
  339. {
  340. this [index].Container = null;
  341. metaParameters.RemoveAt (index);
  342. list.RemoveAt (index);
  343. }
  344. public
  345. #if NET_2_0
  346. override
  347. #endif // NET_2_0
  348. void RemoveAt (string parameterName)
  349. {
  350. RemoveAt (IndexOf (parameterName));
  351. }
  352. #if NET_2_0
  353. public override void AddRange (Array values) {
  354. if (values == null)
  355. throw new ArgumentNullException("The argument passed was null");
  356. foreach ( object value in values) {
  357. if (!(value is SqlParameter))
  358. throw new InvalidCastException ("Element in the array parameter was not an SqlParameter.");
  359. SqlParameter param = (SqlParameter) value;
  360. if (param.Container != null)
  361. throw new ArgumentException ("An SqlParameter specified in the array is already added to this or another SqlParameterCollection.");
  362. param.Container = this;
  363. list.Add (param);
  364. metaParameters.Add (param.MetaParameter);
  365. }
  366. }
  367. public void AddRange (SqlParameter[] values) {
  368. this.AddRange(values);
  369. }
  370. #endif
  371. #endregion // Methods
  372. }
  373. }