OdbcParameter.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. namespace System.Data.Odbc
  13. {
  14. public sealed class OdbcParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
  15. {
  16. #region Fields
  17. string name;
  18. object ParamValue;
  19. int size;
  20. bool isNullable;
  21. byte precision;
  22. byte scale;
  23. DataRowVersion sourceVersion;
  24. string sourceColumn;
  25. ParameterDirection direction;
  26. OdbcType odbcType;
  27. DbType dbType;
  28. OdbcParameterCollection container = null;
  29. // Buffers for parameter value based on type. Currently I've only optimized
  30. // for int parameters and everything else is just converted to a string.
  31. private bool bufferIsSet;
  32. int intbuf;
  33. byte[] buffer;
  34. #endregion
  35. #region Constructors
  36. public OdbcParameter ()
  37. {
  38. name = String.Empty;
  39. ParamValue = null;
  40. size = 0;
  41. isNullable = true;
  42. precision = 0;
  43. scale = 0;
  44. sourceColumn = String.Empty;
  45. }
  46. public OdbcParameter (string name, object value)
  47. : this ()
  48. {
  49. this.name = name;
  50. this.ParamValue = value;
  51. }
  52. public OdbcParameter (string name, OdbcType dataType)
  53. : this ()
  54. {
  55. this.name = name;
  56. OdbcType = dataType;
  57. }
  58. public OdbcParameter (string name, OdbcType dataType, int size)
  59. : this (name, dataType)
  60. {
  61. this.size = size;
  62. }
  63. public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
  64. : this (name, dataType, size)
  65. {
  66. this.sourceColumn = srcColumn;
  67. }
  68. public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
  69. : this (name, dataType, size, srcColumn)
  70. {
  71. this.direction = direction;
  72. this.isNullable = isNullable;
  73. this.precision = precision;
  74. this.scale = scale;
  75. this.sourceVersion = srcVersion;
  76. this.ParamValue = value;
  77. }
  78. #endregion
  79. #region Properties
  80. // Used to ensure that only one collection can contain this
  81. // parameter
  82. internal OdbcParameterCollection Container {
  83. get { return container; }
  84. set { container = value; }
  85. }
  86. public DbType DbType {
  87. get { return dbType; }
  88. set {
  89. dbType = value;
  90. }
  91. }
  92. public ParameterDirection Direction {
  93. get { return direction; }
  94. set { direction = value; }
  95. }
  96. public bool IsNullable {
  97. get { return isNullable; }
  98. set { isNullable = value; }
  99. }
  100. public OdbcType OdbcType {
  101. get { return odbcType; }
  102. set {
  103. odbcType = value;
  104. }
  105. }
  106. public string ParameterName {
  107. get { return name; }
  108. set { name = value; }
  109. }
  110. public byte Precision {
  111. get { return precision; }
  112. set { precision = value; }
  113. }
  114. public byte Scale {
  115. get { return scale; }
  116. set { scale = value; }
  117. }
  118. public int Size {
  119. get { return size; }
  120. set { size = value; }
  121. }
  122. public string SourceColumn {
  123. get { return sourceColumn; }
  124. set { sourceColumn = value; }
  125. }
  126. public DataRowVersion SourceVersion {
  127. get { return sourceVersion; }
  128. set { sourceVersion = value; }
  129. }
  130. public object Value {
  131. get {
  132. return ParamValue;
  133. }
  134. set {
  135. this.ParamValue = value;
  136. bufferIsSet = false;
  137. }
  138. }
  139. #endregion // Properties
  140. #region Methods
  141. internal void Bind(IntPtr hstmt, int ParamNum) {
  142. OdbcReturn ret;
  143. // Set up the buffer if we haven't done so yet
  144. if (!bufferIsSet)
  145. setBuffer();
  146. // Convert System.Data.ParameterDirection into odbc enum
  147. OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.direction);
  148. // Bind parameter based on type
  149. if (odbcType == OdbcType.Int)
  150. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  151. (short)odbcType, (short)odbcType, Convert.ToUInt32(size),
  152. 0, ref intbuf, 0, 0);
  153. else
  154. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  155. (short)OdbcType.Char, (short)odbcType, Convert.ToUInt32(size),
  156. 0, buffer, 0, 0);
  157. // Check for error condition
  158. if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  159. throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
  160. }
  161. private void setBuffer() {
  162. // Load buffer with new value
  163. if (odbcType == OdbcType.Int)
  164. intbuf = (int)ParamValue;
  165. else {
  166. string paramValueString = ParamValue.ToString();
  167. // Treat everything else as a string
  168. // Init string buffer
  169. if (buffer == null || buffer.Length < ((size > 20) ? size : 20))
  170. buffer = new byte[(size > 20) ? size : 20];
  171. else
  172. buffer.Initialize();
  173. // Convert value into string and store into buffer
  174. System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length, buffer, 0);
  175. }
  176. bufferIsSet = true;
  177. }
  178. [MonoTODO]
  179. object ICloneable.Clone ()
  180. {
  181. throw new NotImplementedException ();
  182. }
  183. public override string ToString ()
  184. {
  185. return ParameterName;
  186. }
  187. #endregion
  188. }
  189. }