2
0

OdbcParameter.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
  71. : this (name, dataType, size, srcColumn)
  72. {
  73. this.direction = direction;
  74. this.isNullable = isNullable;
  75. this.precision = precision;
  76. this.scale = scale;
  77. this.sourceVersion = srcVersion;
  78. this.ParamValue = value;
  79. }
  80. #endregion
  81. #region Properties
  82. // Used to ensure that only one collection can contain this
  83. // parameter
  84. internal OdbcParameterCollection Container {
  85. get { return container; }
  86. set { container = value; }
  87. }
  88. [BrowsableAttribute (false)]
  89. [OdbcDescriptionAttribute ("The parameter generic type")]
  90. [RefreshPropertiesAttribute (RefreshProperties.All)]
  91. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  92. [OdbcCategory ("Data")]
  93. public DbType DbType {
  94. get { return dbType; }
  95. set {
  96. dbType = value;
  97. }
  98. }
  99. [OdbcCategory ("Data")]
  100. [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]
  101. [DefaultValue (ParameterDirection.Input)]
  102. public ParameterDirection Direction {
  103. get { return direction; }
  104. set { direction = value; }
  105. }
  106. [BrowsableAttribute (false)]
  107. [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
  108. [DesignOnlyAttribute (true)]
  109. [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
  110. [DefaultValue (false)]
  111. public bool IsNullable {
  112. get { return isNullable; }
  113. set { isNullable = value; }
  114. }
  115. [DefaultValue (OdbcType.NChar)]
  116. [OdbcDescriptionAttribute ("The parameter native type")]
  117. [RefreshPropertiesAttribute (RefreshProperties.All)]
  118. [OdbcCategory ("Data")]
  119. public OdbcType OdbcType {
  120. get { return odbcType; }
  121. set {
  122. odbcType = value;
  123. }
  124. }
  125. public string ParameterName {
  126. get { return name; }
  127. set { name = value; }
  128. }
  129. public byte Precision {
  130. get { return precision; }
  131. set { precision = value; }
  132. }
  133. public byte Scale {
  134. get { return scale; }
  135. set { scale = value; }
  136. }
  137. public int Size {
  138. get { return size; }
  139. set { size = value; }
  140. }
  141. public string SourceColumn {
  142. get { return sourceColumn; }
  143. set { sourceColumn = value; }
  144. }
  145. public DataRowVersion SourceVersion {
  146. get { return sourceVersion; }
  147. set { sourceVersion = value; }
  148. }
  149. public object Value {
  150. get {
  151. return ParamValue;
  152. }
  153. set {
  154. this.ParamValue = value;
  155. bufferIsSet = false;
  156. }
  157. }
  158. #endregion // Properties
  159. #region Methods
  160. internal void Bind(IntPtr hstmt, int ParamNum) {
  161. OdbcReturn ret;
  162. // Set up the buffer if we haven't done so yet
  163. if (!bufferIsSet)
  164. setBuffer();
  165. // Convert System.Data.ParameterDirection into odbc enum
  166. OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.direction);
  167. // Bind parameter based on type
  168. if (odbcType == OdbcType.Int)
  169. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  170. (short)odbcType, (short)odbcType, Convert.ToUInt32(size),
  171. 0, ref intbuf, 0, 0);
  172. else
  173. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  174. (short)OdbcType.Char, (short)odbcType, Convert.ToUInt32(size),
  175. 0, buffer, 0, 0);
  176. // Check for error condition
  177. if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  178. throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
  179. }
  180. private void setBuffer() {
  181. // Load buffer with new value
  182. if (odbcType == OdbcType.Int)
  183. intbuf = (int)ParamValue;
  184. else {
  185. string paramValueString = ParamValue.ToString();
  186. // Treat everything else as a string
  187. // Init string buffer
  188. if (buffer == null || buffer.Length < ((size > 20) ? size : 20))
  189. buffer = new byte[(size > 20) ? size : 20];
  190. else
  191. buffer.Initialize();
  192. // Convert value into string and store into buffer
  193. System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length, buffer, 0);
  194. }
  195. bufferIsSet = true;
  196. }
  197. [MonoTODO]
  198. object ICloneable.Clone ()
  199. {
  200. throw new NotImplementedException ();
  201. }
  202. public override string ToString ()
  203. {
  204. return ParameterName;
  205. }
  206. #endregion
  207. }
  208. }