2
0

SqlParameterCollection.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. //
  10. // (C) Ximian, Inc 2002
  11. // Copyright (C) Tim Coleman, 2002
  12. //
  13. //
  14. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using Mono.Data.Tds;
  36. using System;
  37. using System.ComponentModel;
  38. using System.Data;
  39. using System.Data.Common;
  40. #if NET_2_0
  41. using System.Data.ProviderBase;
  42. #endif // NET_2_0
  43. using System.Collections;
  44. namespace System.Data.SqlClient {
  45. [ListBindable (false)]
  46. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DataParametersEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  47. #if NET_2_0
  48. public sealed class SqlParameterCollection : DbParameterBaseCollection, IDataParameterCollection, IList, ICollection, IEnumerable
  49. #else
  50. public sealed class SqlParameterCollection : MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
  51. #endif // NET_2_0
  52. {
  53. #region Fields
  54. ArrayList list = new ArrayList();
  55. TdsMetaParameterCollection metaParameters;
  56. SqlCommand command;
  57. #endregion // Fields
  58. #region Constructors
  59. internal SqlParameterCollection (SqlCommand command)
  60. {
  61. this.command = command;
  62. metaParameters = new TdsMetaParameterCollection ();
  63. }
  64. #endregion // Constructors
  65. #region Properties
  66. [Browsable (false)]
  67. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  68. public
  69. #if NET_2_0
  70. override
  71. #endif // NET_2_0
  72. int Count {
  73. get { return list.Count; }
  74. }
  75. [Browsable (false)]
  76. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  77. public
  78. #if NET_2_0
  79. new
  80. #endif // NET_2_0
  81. SqlParameter this [int index] {
  82. get { return (SqlParameter) list [index]; }
  83. set { list [index] = (SqlParameter) value; }
  84. }
  85. object IDataParameterCollection.this [string parameterName] {
  86. get { return this[parameterName]; }
  87. set {
  88. if (!(value is SqlParameter))
  89. throw new InvalidCastException ("Only SQLParameter objects can be used.");
  90. this [parameterName] = (SqlParameter) value;
  91. }
  92. }
  93. [Browsable (false)]
  94. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  95. public
  96. #if NET_2_0
  97. new
  98. #endif // NET_2_0
  99. SqlParameter this [string parameterName] {
  100. get {
  101. foreach (SqlParameter p in list)
  102. if (p.ParameterName.Equals (parameterName))
  103. return p;
  104. throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
  105. }
  106. set {
  107. if (!Contains (parameterName))
  108. throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
  109. this [IndexOf (parameterName)] = value;
  110. }
  111. }
  112. object IList.this [int index] {
  113. get { return (SqlParameter) this [index]; }
  114. set { this [index] = (SqlParameter) value; }
  115. }
  116. bool IList.IsFixedSize {
  117. get { return list.IsFixedSize; }
  118. }
  119. bool IList.IsReadOnly {
  120. get { return list.IsReadOnly; }
  121. }
  122. bool ICollection.IsSynchronized {
  123. get { return list.IsSynchronized; }
  124. }
  125. object ICollection.SyncRoot {
  126. get { return list.SyncRoot; }
  127. }
  128. internal TdsMetaParameterCollection MetaParameters {
  129. get { return metaParameters; }
  130. }
  131. #endregion // Properties
  132. #region Methods
  133. public
  134. #if NET_2_0
  135. override
  136. #endif // NET_2_0
  137. int Add (object value)
  138. {
  139. if (!(value is SqlParameter))
  140. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  141. Add ((SqlParameter) value);
  142. return IndexOf (value);
  143. }
  144. public SqlParameter Add (SqlParameter value)
  145. {
  146. if (value.Container != null)
  147. throw new ArgumentException ("The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.");
  148. value.Container = this;
  149. list.Add (value);
  150. metaParameters.Add (value.MetaParameter);
  151. return value;
  152. }
  153. public SqlParameter Add (string parameterName, object value)
  154. {
  155. return Add (new SqlParameter (parameterName, value));
  156. }
  157. public SqlParameter Add (string parameterName, SqlDbType sqlDbType)
  158. {
  159. return Add (new SqlParameter (parameterName, sqlDbType));
  160. }
  161. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size)
  162. {
  163. return Add (new SqlParameter (parameterName, sqlDbType, size));
  164. }
  165. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
  166. {
  167. return Add (new SqlParameter (parameterName, sqlDbType, size, sourceColumn));
  168. }
  169. public
  170. #if NET_2_0
  171. override
  172. #endif // NET_2_0
  173. void Clear()
  174. {
  175. metaParameters.Clear ();
  176. foreach (SqlParameter p in list)
  177. p.Container = null;
  178. list.Clear ();
  179. }
  180. public
  181. #if NET_2_0
  182. override
  183. #endif // NET_2_0
  184. bool Contains (object value)
  185. {
  186. if (!(value is SqlParameter))
  187. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  188. return Contains (((SqlParameter) value).ParameterName);
  189. }
  190. public
  191. #if NET_2_0
  192. override
  193. #endif // NET_2_0
  194. bool Contains (string value)
  195. {
  196. foreach (SqlParameter p in list)
  197. if (p.ParameterName.Equals (value))
  198. return true;
  199. return false;
  200. }
  201. public
  202. #if NET_2_0
  203. override
  204. #endif // NET_2_0
  205. void CopyTo (Array array, int index)
  206. {
  207. list.CopyTo (array, index);
  208. }
  209. public
  210. #if NET_2_0
  211. override
  212. #endif // NET_2_0
  213. IEnumerator GetEnumerator()
  214. {
  215. return list.GetEnumerator ();
  216. }
  217. public
  218. #if NET_2_0
  219. override
  220. #endif // NET_2_0
  221. int IndexOf (object value)
  222. {
  223. if (!(value is SqlParameter))
  224. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  225. return IndexOf (((SqlParameter) value).ParameterName);
  226. }
  227. public
  228. #if NET_2_0
  229. override
  230. #endif // NET_2_0
  231. int IndexOf (string parameterName)
  232. {
  233. for (int i = 0; i < Count; i += 1)
  234. if (this [i].ParameterName.Equals (parameterName))
  235. return i;
  236. return -1;
  237. }
  238. public
  239. #if NET_2_0
  240. override
  241. #endif // NET_2_0
  242. void Insert (int index, object value)
  243. {
  244. list.Insert (index, value);
  245. }
  246. public
  247. #if NET_2_0
  248. override
  249. #endif // NET_2_0
  250. void Remove (object value)
  251. {
  252. ((SqlParameter) value).Container = null;
  253. metaParameters.Remove (((SqlParameter) value).MetaParameter);
  254. list.Remove (value);
  255. }
  256. public
  257. #if NET_2_0
  258. override
  259. #endif // NET_2_0
  260. void RemoveAt (int index)
  261. {
  262. this [index].Container = null;
  263. metaParameters.RemoveAt (index);
  264. list.RemoveAt (index);
  265. }
  266. public
  267. #if NET_2_0
  268. override
  269. #endif // NET_2_0
  270. void RemoveAt (string parameterName)
  271. {
  272. RemoveAt (IndexOf (parameterName));
  273. }
  274. [MonoTODO]
  275. protected
  276. #if NET_2_0
  277. override
  278. #endif // NET_2_0
  279. Type ItemType
  280. {
  281. get {throw new NotImplementedException ();}
  282. }
  283. #endregion // Methods
  284. }
  285. }