OciServerHandle.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // OciServerHandle.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. namespace System.Data.OracleClient.Oci {
  20. internal sealed class OciServerHandle : OciHandle, IDisposable
  21. {
  22. #region Fields
  23. bool disposed = false;
  24. bool attached = false;
  25. OciErrorHandle errorHandle;
  26. #endregion // Fields
  27. #region Constructors
  28. public OciServerHandle (OciHandle parent, IntPtr newHandle)
  29. : base (OciHandleType.Server, parent, newHandle)
  30. {
  31. }
  32. #endregion // Constructors
  33. #region Methods
  34. [DllImport ("oci")]
  35. static extern int OCIServerAttach (IntPtr srvhp,
  36. IntPtr errhp,
  37. string dblink,
  38. [MarshalAs (UnmanagedType.U4)] int dblink_len,
  39. uint mode);
  40. [DllImport ("oci")]
  41. static extern int OCIServerDetach (IntPtr srvhp,
  42. IntPtr errhp,
  43. uint mode);
  44. public bool Attach (string tnsname, OciErrorHandle error)
  45. {
  46. errorHandle = error;
  47. int status = OCIServerAttach (this, error, tnsname, tnsname.Length, 0);
  48. if (status != 0) {
  49. OciErrorInfo info = errorHandle.HandleError ();
  50. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  51. }
  52. attached = true;
  53. return attached;
  54. }
  55. public void Detach (OciErrorHandle error)
  56. {
  57. if (!attached)
  58. return;
  59. int status = OCIServerDetach (this, error, 0);
  60. if (status != 0) {
  61. OciErrorInfo info = errorHandle.HandleError ();
  62. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  63. }
  64. attached = false;
  65. }
  66. protected override void Dispose (bool disposing)
  67. {
  68. if (!disposed) {
  69. try {
  70. Detach (errorHandle);
  71. disposed = true;
  72. } finally {
  73. base.Dispose (disposing);
  74. }
  75. }
  76. }
  77. #endregion // Methods
  78. }
  79. }