OciBindHandle.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // OciBindHandle.cs
  3. //
  4. // Part of managed C#/.NET library System.Data.OracleClient.dll
  5. //
  6. // Part of the Mono class libraries at
  7. // mcs/class/System.Data.OracleClient/System.Data.OracleClient.Oci
  8. //
  9. // Assembly: System.Data.OracleClient.dll
  10. // Namespace: System.Data.OracleClient.Oci
  11. //
  12. // Author:
  13. // Tim Coleman <[email protected]>
  14. //
  15. // Copyright (C) Tim Coleman, 2003
  16. //
  17. using System;
  18. using System.Runtime.InteropServices;
  19. using System.Text;
  20. namespace System.Data.OracleClient.Oci {
  21. internal sealed class OciBindHandle : IOciHandle, IDisposable
  22. {
  23. #region Fields
  24. IntPtr handle;
  25. string name;
  26. IntPtr value;
  27. OciStatementHandle statement;
  28. OciDataType type;
  29. int size;
  30. int indicator;
  31. #endregion // Fields
  32. #region Constructors
  33. public OciBindHandle (string name)
  34. {
  35. this.name = name;
  36. this.value = IntPtr.Zero;
  37. this.indicator = 0;
  38. }
  39. #endregion // Constructors
  40. #region Properties
  41. public IntPtr Handle {
  42. get { return handle; }
  43. set { handle = value; }
  44. }
  45. public OciHandleType HandleType {
  46. get { return OciHandleType.Bind; }
  47. }
  48. public string Name {
  49. get { return name; }
  50. set { name = value; }
  51. }
  52. public int Size {
  53. get { return size; }
  54. set { size = value; }
  55. }
  56. public OciDataType Type {
  57. get { return type; }
  58. set { type = value; }
  59. }
  60. public IntPtr Value {
  61. get { return value; }
  62. }
  63. #endregion
  64. #region Methods
  65. [DllImport ("oci", EntryPoint = "OCIBindByName")]
  66. public static extern int OCIBindByName (IntPtr stmtp,
  67. out IntPtr bindpp,
  68. IntPtr errhp,
  69. string placeholder,
  70. int placeh_len,
  71. IntPtr valuep,
  72. int value_sz,
  73. [MarshalAs (UnmanagedType.U2)] OciDataType dty,
  74. ref int indp,
  75. IntPtr alenp,
  76. ushort rcodep,
  77. uint maxarr_len,
  78. IntPtr curelp,
  79. uint mode);
  80. public void Bind (OciStatementHandle statement, object val)
  81. {
  82. Console.WriteLine ("IN BIND");
  83. handle = IntPtr.Zero;
  84. this.statement = statement;
  85. int indp = 0;
  86. ushort alenp = 0;
  87. IntPtr rcodep = IntPtr.Zero;
  88. int status = 0;
  89. OciDataType bindType = Type;
  90. int definedSize = 0;
  91. string stringValue = val.ToString ();
  92. if (val == DBNull.Value)
  93. indicator = -1;
  94. else {
  95. switch (Type) {
  96. case OciDataType.Number:
  97. case OciDataType.Integer:
  98. case OciDataType.Float:
  99. case OciDataType.VarNum:
  100. bindType = OciDataType.Char;
  101. definedSize = stringValue.Length;
  102. value = Marshal.StringToHGlobalAnsi (stringValue);
  103. break;
  104. case OciDataType.Date:
  105. break;
  106. default:
  107. bindType = OciDataType.Char;
  108. definedSize = stringValue.Length;
  109. value = Marshal.StringToHGlobalAnsi (stringValue);
  110. break;
  111. }
  112. }
  113. status = OCIBindByName (statement.Handle,
  114. out handle,
  115. statement.ErrorHandle.Handle,
  116. name,
  117. name.Length,
  118. value,
  119. definedSize,
  120. bindType,
  121. ref indicator,
  122. IntPtr.Zero,
  123. 0,
  124. 0,
  125. IntPtr.Zero,
  126. 0);
  127. if (status != 0) {
  128. OciErrorInfo info = statement.ErrorHandle.HandleError ();
  129. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  130. }
  131. }
  132. public void Dispose ()
  133. {
  134. Marshal.FreeHGlobal (value);
  135. }
  136. #endregion // Methods
  137. }
  138. }