OciSessionHandle.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // OciSessionHandle.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 OciSessionHandle : OciHandle, IDisposable
  21. {
  22. #region Fields
  23. OciErrorHandle errorHandle;
  24. OciServiceHandle serviceHandle;
  25. bool begun = false;
  26. bool disposed = false;
  27. string username;
  28. string password;
  29. #endregion // Fields
  30. #region Constructors
  31. public OciSessionHandle (OciHandle parent, IntPtr handle)
  32. : base (OciHandleType.Session, parent, handle)
  33. {
  34. }
  35. #endregion // Constructors
  36. #region Properties
  37. public OciServiceHandle Service {
  38. get { return serviceHandle; }
  39. set { serviceHandle = value; }
  40. }
  41. public string Username {
  42. get { return username; }
  43. set { username = value; }
  44. }
  45. public string Password {
  46. get { return password; }
  47. set { password = value; }
  48. }
  49. #endregion // Properties
  50. #region Methods
  51. [DllImport ("oci")]
  52. public static extern int OCISessionBegin (IntPtr svchp,
  53. IntPtr errhp,
  54. IntPtr usrhp,
  55. [MarshalAs (UnmanagedType.U4)] OciCredentialType credt,
  56. [MarshalAs (UnmanagedType.U4)] OciSessionMode mode);
  57. [DllImport ("oci")]
  58. public static extern int OCISessionEnd (IntPtr svchp,
  59. IntPtr errhp,
  60. IntPtr usrhp,
  61. uint mode);
  62. public bool BeginSession (OciCredentialType credentialType, OciSessionMode mode, OciErrorHandle error)
  63. {
  64. errorHandle = error;
  65. int status = 0;
  66. status = OciGlue.OCIAttrSetString (this,
  67. OciHandleType.Session,
  68. username,
  69. (uint) username.Length,
  70. OciAttributeType.Username,
  71. errorHandle);
  72. if (status != 0)
  73. return false;
  74. status = OciGlue.OCIAttrSetString (this,
  75. OciHandleType.Session,
  76. password,
  77. (uint) password.Length,
  78. OciAttributeType.Password,
  79. errorHandle);
  80. if (status != 0)
  81. return false;
  82. status = OCISessionBegin (Service,
  83. errorHandle,
  84. Handle,
  85. credentialType,
  86. mode);
  87. if (status != 0)
  88. return false;
  89. begun = true;
  90. return true;
  91. }
  92. public void EndSession (OciErrorHandle error)
  93. {
  94. if (!begun)
  95. return;
  96. OCISessionEnd (Service, error, this, 0);
  97. }
  98. protected override void Dispose (bool disposing)
  99. {
  100. if (!disposed) {
  101. try {
  102. EndSession (errorHandle);
  103. disposed = false;
  104. } finally {
  105. base.Dispose (disposing);
  106. }
  107. }
  108. }
  109. #endregion // Methods
  110. }
  111. }