OdbcParameter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. }
  99. public OdbcType OdbcType {
  100. get { return odbcType; }
  101. set {
  102. odbcType = value;
  103. }
  104. }
  105. public string ParameterName {
  106. get { return name; }
  107. set { name = value; }
  108. }
  109. public byte Precision {
  110. get { return precision; }
  111. set { precision = value; }
  112. }
  113. public byte Scale {
  114. get { return scale; }
  115. set { scale = value; }
  116. }
  117. public int Size {
  118. get { return size; }
  119. set { size = value; }
  120. }
  121. public string SourceColumn {
  122. get { return sourceColumn; }
  123. set { sourceColumn = value; }
  124. }
  125. public DataRowVersion SourceVersion {
  126. get { return sourceVersion; }
  127. set { sourceVersion = value; }
  128. }
  129. public object Value {
  130. get {
  131. return ParamValue;
  132. }
  133. set {
  134. this.ParamValue = value;
  135. bufferIsSet = false;
  136. }
  137. }
  138. #endregion // Properties
  139. #region public Properties
  140. public void Bind(IntPtr hstmt,int ParamNum)
  141. {
  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. {
  167. string paramValueString = ParamValue.ToString();
  168. // Treat everything else as a string
  169. // Init string buffer
  170. if (buffer==null || buffer.Length< ((size>20)?size:20) )
  171. buffer=new byte[(size>20)?size:20];
  172. else
  173. buffer.Initialize();
  174. // Convert value into string and store into buffer
  175. System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length , buffer, 0);
  176. }
  177. bufferIsSet = true;
  178. }
  179. #endregion // public Properties
  180. #region Methods
  181. [MonoTODO]
  182. object ICloneable.Clone ()
  183. {
  184. throw new NotImplementedException ();
  185. }
  186. public override string ToString ()
  187. {
  188. return ParameterName;
  189. }
  190. #endregion
  191. }
  192. }