OdbcError.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // System.Data.Odbc.OdbcError
  3. //
  4. // Author:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 2002
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Collections;
  32. using System.ComponentModel;
  33. using System.Data;
  34. using System.Data.Common;
  35. namespace System.Data.Odbc
  36. {
  37. [Serializable]
  38. public sealed class OdbcError
  39. {
  40. string _message;
  41. string _source;
  42. string _state;
  43. int _nativeerror;
  44. #region Constructors
  45. internal OdbcError(string Source)
  46. {
  47. _nativeerror = 1;
  48. _source = Source;
  49. _message = "Error in " + _source;
  50. _state = "";
  51. }
  52. internal OdbcError(string Source, OdbcHandleType HandleType, IntPtr Handle)
  53. {
  54. short buflen=256,txtlen=0;
  55. OdbcReturn ret=OdbcReturn.Success;
  56. byte[] buf_MsgText=new byte[buflen];
  57. byte[] buf_SqlState=new byte[buflen];
  58. bool NeedsDecode=true;
  59. _source = Source;
  60. switch (HandleType)
  61. {
  62. case OdbcHandleType.Dbc:
  63. ret=libodbc.SQLError(IntPtr.Zero,Handle,IntPtr.Zero, buf_SqlState,
  64. ref _nativeerror, buf_MsgText, buflen, ref txtlen);
  65. break;
  66. case OdbcHandleType.Stmt:
  67. ret=libodbc.SQLError(IntPtr.Zero,IntPtr.Zero,Handle, buf_SqlState,
  68. ref _nativeerror, buf_MsgText, buflen, ref txtlen);
  69. break;
  70. case OdbcHandleType.Env:
  71. ret=libodbc.SQLError(Handle,IntPtr.Zero,IntPtr.Zero, buf_SqlState,
  72. ref _nativeerror, buf_MsgText, buflen, ref txtlen);
  73. break;
  74. default:
  75. _nativeerror = 1;
  76. _source = Source;
  77. _message = "Error in " + _source;
  78. _state = "";
  79. NeedsDecode=false;
  80. break;
  81. }
  82. if (NeedsDecode)
  83. {
  84. if (ret!=OdbcReturn.Success)
  85. {
  86. _nativeerror = 1;
  87. _source = Source;
  88. _message = "Unable to retreive error information from ODBC driver manager";
  89. _state = "";
  90. }
  91. else
  92. {
  93. _state = System.Text.Encoding.Default.GetString (buf_SqlState).Replace ((char) 0, ' ').Trim ();
  94. _message = System.Text.Encoding.Default.GetString (buf_MsgText).Replace ((char) 0, ' ').Trim ();
  95. }
  96. }
  97. }
  98. #endregion // Constructors
  99. #region Properties
  100. public string Message
  101. {
  102. get
  103. {
  104. return _message;
  105. }
  106. }
  107. public int NativeError
  108. {
  109. get
  110. {
  111. return _nativeerror;
  112. }
  113. }
  114. public string Source
  115. {
  116. get
  117. {
  118. return _source;
  119. }
  120. }
  121. public string SQLState
  122. {
  123. get
  124. {
  125. return _state;
  126. }
  127. }
  128. #endregion // Properties
  129. #region methods
  130. public override string ToString ()
  131. {
  132. return Message;
  133. }
  134. #endregion
  135. }
  136. }