OciErrorHandle.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // OciErrorHandle.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 OciErrorHandle : OciHandle, IDisposable
  22. {
  23. #region Fields
  24. bool disposed = false;
  25. #endregion // Fields
  26. #region Constructors
  27. public OciErrorHandle (OciHandle parent, IntPtr newHandle)
  28. : base (OciHandleType.Error, parent, newHandle)
  29. {
  30. }
  31. #endregion // Constructors
  32. #region Methods
  33. protected override void Dispose (bool disposing)
  34. {
  35. if (!disposed) {
  36. disposed = true;
  37. base.Dispose (disposing);
  38. }
  39. }
  40. public static OciErrorInfo HandleError (OciHandle hand)
  41. {
  42. OciErrorInfo info;
  43. info.ErrorCode = 0;
  44. info.ErrorMessage = String.Empty;
  45. int errbufSize = 4096;
  46. IntPtr errbuf = Marshal.AllocHGlobal (errbufSize);
  47. OciCalls.OCIErrorGet (hand,
  48. 1,
  49. IntPtr.Zero,
  50. out info.ErrorCode,
  51. errbuf,
  52. (uint) errbufSize,
  53. OciHandleType.Error);
  54. byte[] bytea = new byte[errbufSize];
  55. Marshal.Copy (errbuf, bytea, 0, errbufSize);
  56. errbufSize = 0;
  57. OciHandle h = hand.Parent;
  58. if (h == null)
  59. h = hand;
  60. // first call to OCICharSetToUnicode gets the size
  61. OciCalls.OCICharSetToUnicode (h, null, bytea, out errbufSize);
  62. StringBuilder str = new StringBuilder (errbufSize);
  63. // second call to OCICharSetToUnicode gets the string
  64. OciCalls.OCICharSetToUnicode (h, str, bytea, out errbufSize);
  65. string errmsg = String.Empty;
  66. if (errbufSize > 0)
  67. errmsg = str.ToString ();
  68. info.ErrorMessage = String.Copy (errmsg);
  69. Marshal.FreeHGlobal (errbuf);
  70. return info;
  71. }
  72. public OciErrorInfo HandleError ()
  73. {
  74. return HandleError (this);
  75. }
  76. #endregion // Methods
  77. }
  78. }