SqlException.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // System.Data.SqlClient.SqlException.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. // Tim Coleman ([email protected])
  8. //
  9. // (C) Ximian, Inc
  10. // Copyright (C) Tim Coleman, 2002
  11. //
  12. //
  13. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.ComponentModel;
  36. using System.Data;
  37. using System.Data.Common;
  38. using System.Runtime.Serialization;
  39. using System.Text;
  40. using System.Diagnostics;
  41. using System.Globalization;
  42. using Mono.Data.Tds.Protocol;
  43. namespace System.Data.SqlClient
  44. {
  45. [Serializable]
  46. public sealed class SqlException : DbException
  47. {
  48. #region ReferenceSource
  49. internal SqlException InternalClone() {
  50. var ret = new SqlException ();
  51. foreach (SqlError e in errors)
  52. ret.errors.Add (e);
  53. return ret;
  54. }
  55. static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion) {
  56. return CreateException(errorCollection, serverVersion, Guid.Empty);
  57. }
  58. static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, SqlInternalConnectionTds internalConnection, Exception innerException = null) {
  59. Guid connectionId = Guid.Empty;
  60. var exception = CreateException(errorCollection, serverVersion, connectionId, innerException);
  61. /*
  62. if (internalConnection != null) {
  63. if ((internalConnection.OriginalClientConnectionId != Guid.Empty) && (internalConnection.OriginalClientConnectionId != internalConnection.ClientConnectionId)) {
  64. exception.Data.Add(OriginalClientConnectionIdKey, internalConnection.OriginalClientConnectionId);
  65. }
  66. if (!string.IsNullOrEmpty(internalConnection.RoutingDestination)) {
  67. exception.Data.Add(RoutingDestinationKey, internalConnection.RoutingDestination);
  68. }
  69. }
  70. */
  71. return exception;
  72. }
  73. static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, Guid conId, Exception innerException = null) {
  74. Debug.Assert(null != errorCollection && errorCollection.Count > 0, "no errorCollection?");
  75. // concat all messages together MDAC 65533
  76. StringBuilder message = new StringBuilder();
  77. for (int i = 0; i < errorCollection.Count; i++) {
  78. if (i > 0) {
  79. message.Append(Environment.NewLine);
  80. }
  81. message.Append(errorCollection[i].Message);
  82. }
  83. if (innerException == null && errorCollection[0].Win32ErrorCode != 0 && errorCollection[0].Win32ErrorCode != -1) {
  84. innerException = new Win32Exception(errorCollection[0].Win32ErrorCode);
  85. }
  86. SqlException exception = new SqlException(message.ToString(), /*errorCollection, */innerException/*, conId*/);
  87. exception.Data.Add("HelpLink.ProdName", "Microsoft SQL Server");
  88. if (!ADP.IsEmpty(serverVersion)) {
  89. exception.Data.Add("HelpLink.ProdVer", serverVersion);
  90. }
  91. exception.Data.Add("HelpLink.EvtSrc", "MSSQLServer");
  92. exception.Data.Add("HelpLink.EvtID", errorCollection[0].Number.ToString(CultureInfo.InvariantCulture));
  93. exception.Data.Add("HelpLink.BaseHelpUrl", "http://go.microsoft.com/fwlink");
  94. exception.Data.Add("HelpLink.LinkId", "20476");
  95. return exception;
  96. }
  97. internal bool _doNotReconnect = false;
  98. #endregion
  99. #region Fields
  100. private readonly SqlErrorCollection errors;
  101. private const string DEF_MESSAGE = "SQL Exception has occured.";
  102. #endregion // Fields
  103. #region Constructors
  104. internal SqlException ()
  105. : this (DEF_MESSAGE, null, null)
  106. {
  107. }
  108. internal SqlException (string message, Exception inner)
  109. : this (message, inner, null)
  110. {
  111. }
  112. internal SqlException (string message, Exception inner, SqlError sqlError)
  113. : base (message == null ? DEF_MESSAGE : message, inner)
  114. {
  115. HResult = -2146232060;
  116. errors = new SqlErrorCollection ();
  117. if (sqlError != null)
  118. errors.Add (sqlError);
  119. }
  120. internal SqlException (byte theClass, int lineNumber, string message, int number, string procedure, string server, string source, byte state)
  121. : this (null,
  122. null,
  123. new SqlError (number, state, theClass, server, message, procedure, lineNumber, 0))
  124. {
  125. }
  126. private SqlException(SerializationInfo si, StreamingContext sc)
  127. {
  128. HResult = -2146232060;
  129. errors = (SqlErrorCollection) si.GetValue ("Errors", typeof (SqlErrorCollection));
  130. }
  131. #endregion // Constructors
  132. #region Properties
  133. public byte Class {
  134. get { return Errors [0].Class; }
  135. }
  136. [MonoTODO]
  137. public Guid ClientConnectionId {
  138. get { throw new NotImplementedException (); }
  139. }
  140. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  141. public SqlErrorCollection Errors {
  142. get { return errors; }
  143. }
  144. public int LineNumber {
  145. get { return Errors [0].LineNumber; }
  146. }
  147. public override string Message {
  148. get {
  149. if (Errors.Count == 0)
  150. return base.Message;
  151. StringBuilder result = new StringBuilder ();
  152. if (base.Message != DEF_MESSAGE) {
  153. result.Append (base.Message);
  154. result.Append ("\n");
  155. }
  156. for (int i = 0; i < Errors.Count -1; i++) {
  157. result.Append (Errors [i].Message);
  158. result.Append ("\n");
  159. }
  160. result.Append (Errors [Errors.Count - 1].Message);
  161. return result.ToString ();
  162. }
  163. }
  164. public int Number {
  165. get { return Errors [0].Number; }
  166. }
  167. public string Procedure {
  168. get { return Errors [0].Procedure; }
  169. }
  170. public string Server {
  171. get { return Errors [0].Server; }
  172. }
  173. public override string Source {
  174. get { return Errors [0].Source; }
  175. }
  176. public byte State {
  177. get { return Errors [0].State; }
  178. }
  179. #endregion // Properties
  180. #region Methods
  181. internal static SqlException FromTdsInternalException (TdsInternalException e)
  182. {
  183. return new SqlException (e.Class, e.LineNumber, e.Message,
  184. e.Number, e.Procedure, e.Server,
  185. "Mono SqlClient Data Provider", e.State);
  186. }
  187. public override void GetObjectData (SerializationInfo si, StreamingContext context)
  188. {
  189. if (si == null)
  190. throw new ArgumentNullException ("si");
  191. si.AddValue ("Errors", errors, typeof(SqlErrorCollection));
  192. base.GetObjectData (si, context);
  193. }
  194. #endregion // Methods
  195. }
  196. }