OdbcParameterCollection.cs 9.8 KB

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