OdbcParameter.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // System.Data.Odbc.OdbcParameter
  3. //
  4. // Authors:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 2002
  8. //
  9. using System;
  10. using System.Data;
  11. using System.Data.Common;
  12. using System.ComponentModel;
  13. namespace System.Data.Odbc
  14. {
  15. [TypeConverterAttribute (typeof (OdbcParameterConverter))]
  16. public sealed class OdbcParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
  17. {
  18. #region Fields
  19. string name;
  20. object ParamValue;
  21. int size;
  22. bool isNullable;
  23. byte precision;
  24. byte scale;
  25. DataRowVersion sourceVersion;
  26. string sourceColumn;
  27. ParameterDirection direction;
  28. OdbcType odbcType;
  29. DbType dbType;
  30. OdbcParameterCollection container = null;
  31. // Buffers for parameter value based on type. Currently I've only optimized
  32. // for int parameters and everything else is just converted to a string.
  33. private bool bufferIsSet;
  34. int intbuf;
  35. byte[] buffer;
  36. #endregion
  37. #region Constructors
  38. public OdbcParameter ()
  39. {
  40. name = String.Empty;
  41. ParamValue = null;
  42. size = 0;
  43. isNullable = true;
  44. precision = 0;
  45. scale = 0;
  46. sourceColumn = String.Empty;
  47. }
  48. public OdbcParameter (string name, object value)
  49. : this ()
  50. {
  51. this.name = name;
  52. this.ParamValue = value;
  53. }
  54. public OdbcParameter (string name, OdbcType dataType)
  55. : this ()
  56. {
  57. this.name = name;
  58. OdbcType = dataType;
  59. }
  60. public OdbcParameter (string name, OdbcType dataType, int size)
  61. : this (name, dataType)
  62. {
  63. this.size = size;
  64. }
  65. public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
  66. : this (name, dataType, size)
  67. {
  68. this.sourceColumn = srcColumn;
  69. }
  70. [EditorBrowsable (EditorBrowsableState.Advanced)]
  71. public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
  72. : this (name, dataType, size, srcColumn)
  73. {
  74. this.direction = direction;
  75. this.isNullable = isNullable;
  76. this.precision = precision;
  77. this.scale = scale;
  78. this.sourceVersion = srcVersion;
  79. this.ParamValue = value;
  80. }
  81. #endregion
  82. #region Properties
  83. // Used to ensure that only one collection can contain this
  84. // parameter
  85. internal OdbcParameterCollection Container {
  86. get { return container; }
  87. set { container = value; }
  88. }
  89. [BrowsableAttribute (false)]
  90. [OdbcDescriptionAttribute ("The parameter generic type")]
  91. [RefreshPropertiesAttribute (RefreshProperties.All)]
  92. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  93. [OdbcCategory ("Data")]
  94. public DbType DbType {
  95. get { return dbType; }
  96. set {
  97. dbType = value;
  98. }
  99. }
  100. [OdbcCategory ("Data")]
  101. [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]
  102. [DefaultValue (ParameterDirection.Input)]
  103. public ParameterDirection Direction {
  104. get { return direction; }
  105. set { direction = value; }
  106. }
  107. [BrowsableAttribute (false)]
  108. [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
  109. [DesignOnlyAttribute (true)]
  110. [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
  111. [DefaultValue (false)]
  112. public bool IsNullable {
  113. get { return isNullable; }
  114. set { isNullable = value; }
  115. }
  116. [DefaultValue (OdbcType.NChar)]
  117. [OdbcDescriptionAttribute ("The parameter native type")]
  118. [RefreshPropertiesAttribute (RefreshProperties.All)]
  119. [OdbcCategory ("Data")]
  120. public OdbcType OdbcType {
  121. get { return odbcType; }
  122. set {
  123. odbcType = value;
  124. }
  125. }
  126. [OdbcDescription ("DataParameter_ParameterName")]
  127. [DefaultValue ("")]
  128. public string ParameterName {
  129. get { return name; }
  130. set { name = value; }
  131. }
  132. [OdbcDescription ("DbDataParameter_Precision")]
  133. [OdbcCategory ("DataCategory_Data")]
  134. [DefaultValue (0)]
  135. public byte Precision {
  136. get { return precision; }
  137. set { precision = value; }
  138. }
  139. [OdbcDescription ("DbDataParameter_Scale")]
  140. [OdbcCategory ("DataCategory_Data")]
  141. [DefaultValue (0)]
  142. public byte Scale {
  143. get { return scale; }
  144. set { scale = value; }
  145. }
  146. [OdbcDescription ("DbDataParameter_Size")]
  147. [OdbcCategory ("DataCategory_Data")]
  148. [DefaultValue (0)]
  149. public int Size {
  150. get { return size; }
  151. set { size = value; }
  152. }
  153. [OdbcDescription ("DataParameter_SourceColumn")]
  154. [OdbcCategory ("DataCategory_Data")]
  155. [DefaultValue ("")]
  156. public string SourceColumn {
  157. get { return sourceColumn; }
  158. set { sourceColumn = value; }
  159. }
  160. [OdbcDescription ("DataParameter_SourceVersion")]
  161. [OdbcCategory ("DataCategory_Data")]
  162. [DefaultValue (512)]
  163. public DataRowVersion SourceVersion {
  164. get { return sourceVersion; }
  165. set { sourceVersion = value; }
  166. }
  167. [TypeConverter (typeof(StringConverter))]
  168. [OdbcDescription ("DataParameter_Value")]
  169. [OdbcCategory ("DataCategory_Data")]
  170. [DefaultValue (null)]
  171. public object Value {
  172. get {
  173. return ParamValue;
  174. }
  175. set {
  176. this.ParamValue = value;
  177. bufferIsSet = false;
  178. }
  179. }
  180. #endregion // Properties
  181. #region Methods
  182. internal void Bind(IntPtr hstmt, int ParamNum) {
  183. OdbcReturn ret;
  184. // Set up the buffer if we haven't done so yet
  185. if (!bufferIsSet)
  186. setBuffer();
  187. // Convert System.Data.ParameterDirection into odbc enum
  188. OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.direction);
  189. // Bind parameter based on type
  190. if (odbcType == OdbcType.Int)
  191. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  192. (short)odbcType, (short)odbcType, Convert.ToUInt32(size),
  193. 0, ref intbuf, 0, 0);
  194. else
  195. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  196. (short)OdbcType.Char, (short)odbcType, Convert.ToUInt32(size),
  197. 0, buffer, 0, 0);
  198. // Check for error condition
  199. if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  200. throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
  201. }
  202. private void setBuffer() {
  203. // Load buffer with new value
  204. if (odbcType == OdbcType.Int)
  205. intbuf = (int)ParamValue;
  206. else {
  207. string paramValueString = ParamValue.ToString();
  208. // Treat everything else as a string
  209. // Init string buffer
  210. if (buffer == null || buffer.Length < ((size > 20) ? size : 20))
  211. buffer = new byte[(size > 20) ? size : 20];
  212. else
  213. buffer.Initialize();
  214. // Convert value into string and store into buffer
  215. System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length, buffer, 0);
  216. }
  217. bufferIsSet = true;
  218. }
  219. [MonoTODO]
  220. object ICloneable.Clone ()
  221. {
  222. throw new NotImplementedException ();
  223. }
  224. public override string ToString ()
  225. {
  226. return ParameterName;
  227. }
  228. #endregion
  229. }
  230. }