OdbcParameter.cs 4.7 KB

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