OdbcException.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // System.Data.Odbc.OdbcException
  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. using System.Globalization;
  36. using System.Runtime.InteropServices;
  37. using System.Runtime.Serialization;
  38. using System.Text;
  39. namespace System.Data.Odbc
  40. {
  41. [Serializable]
  42. #if NET_2_0
  43. public sealed class OdbcException : DbException
  44. #else
  45. public sealed class OdbcException : SystemException
  46. #endif
  47. {
  48. OdbcErrorCollection odbcErrors;
  49. internal OdbcException (OdbcErrorCollection errors)
  50. : base (CreateMessage (errors))
  51. {
  52. odbcErrors = errors;
  53. }
  54. public OdbcErrorCollection Errors {
  55. get {
  56. return odbcErrors;
  57. }
  58. }
  59. public override string Source {
  60. get {
  61. return odbcErrors [0].Source;
  62. }
  63. }
  64. #if !NET_2_0
  65. public override string Message {
  66. get {
  67. return base.Message;
  68. }
  69. }
  70. #endif
  71. private OdbcException (SerializationInfo si, StreamingContext sc) : base(si, sc)
  72. {
  73. odbcErrors = new OdbcErrorCollection ();
  74. odbcErrors = ((OdbcErrorCollection) si.GetValue (
  75. "odbcErrors", typeof (OdbcErrorCollection)));
  76. }
  77. #region Methods
  78. public override void GetObjectData (SerializationInfo si, StreamingContext context)
  79. {
  80. if (si == null)
  81. throw new ArgumentNullException ("si");
  82. si.AddValue ("odbcErrors", odbcErrors, typeof(OdbcErrorCollection));
  83. base.GetObjectData (si, context);
  84. }
  85. static string CreateMessage (OdbcErrorCollection errors)
  86. {
  87. StringBuilder sb = new StringBuilder ();
  88. for (int i = 0; i < errors.Count; i++) {
  89. if (i > 0)
  90. sb.Append (Environment.NewLine);
  91. OdbcError error = errors [i];
  92. sb.AppendFormat ("ERROR [{0}] {1}", error.SQLState,
  93. error.Message);
  94. }
  95. return sb.ToString ();
  96. }
  97. #endregion // Methods
  98. }
  99. }