OdbcParameter.cs 4.5 KB

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