OciSessionHandle.cs 2.7 KB

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