SqlException.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //------------------------------------------------------------------------------
  2. // <copyright file="SqlException.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. // <owner current="true" primary="false">[....]</owner>
  7. //------------------------------------------------------------------------------
  8. namespace System.Data.SqlClient {
  9. using System;
  10. using System.Collections;
  11. using System.ComponentModel;
  12. using System.Data.Common;
  13. using System.Diagnostics;
  14. using System.Globalization;
  15. using System.Runtime.Serialization;
  16. using System.Text; // StringBuilder
  17. [Serializable]
  18. public sealed class SqlException : System.Data.Common.DbException {
  19. private const string OriginalClientConnectionIdKey = "OriginalClientConnectionId";
  20. private const string RoutingDestinationKey = "RoutingDestination";
  21. private SqlErrorCollection _errors;
  22. [System.Runtime.Serialization.OptionalFieldAttribute(VersionAdded = 4)]
  23. private Guid _clientConnectionId = Guid.Empty;
  24. private SqlException(string message, SqlErrorCollection errorCollection, Exception innerException, Guid conId) : base(message, innerException) {
  25. HResult = HResults.SqlException;
  26. _errors = errorCollection;
  27. _clientConnectionId = conId;
  28. }
  29. // runtime will call even if private...
  30. private SqlException(SerializationInfo si, StreamingContext sc) : base(si, sc) {
  31. _errors = (SqlErrorCollection) si.GetValue("Errors", typeof(SqlErrorCollection));
  32. HResult = HResults.SqlException;
  33. foreach (SerializationEntry siEntry in si) {
  34. if ("ClientConnectionId" == siEntry.Name) {
  35. _clientConnectionId = (Guid)siEntry.Value;
  36. break;
  37. }
  38. }
  39. }
  40. [System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Flags=System.Security.Permissions.SecurityPermissionFlag.SerializationFormatter)]
  41. override public void GetObjectData(SerializationInfo si, StreamingContext context) {
  42. if (null == si) {
  43. throw new ArgumentNullException("si");
  44. }
  45. si.AddValue("Errors", _errors, typeof(SqlErrorCollection));
  46. si.AddValue("ClientConnectionId", _clientConnectionId, typeof(Guid));
  47. base.GetObjectData(si, context);
  48. }
  49. [
  50. DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
  51. ]
  52. public SqlErrorCollection Errors {
  53. get {
  54. if (_errors == null) {
  55. _errors = new SqlErrorCollection();
  56. }
  57. return _errors;
  58. }
  59. }
  60. public Guid ClientConnectionId {
  61. get {
  62. return this._clientConnectionId;
  63. }
  64. }
  65. /*virtual protected*/private bool ShouldSerializeErrors() { // MDAC 65548
  66. return ((null != _errors) && (0 < _errors.Count));
  67. }
  68. public byte Class {
  69. get { return this.Errors[0].Class;}
  70. }
  71. public int LineNumber {
  72. get { return this.Errors[0].LineNumber;}
  73. }
  74. public int Number {
  75. get { return this.Errors[0].Number;}
  76. }
  77. public string Procedure {
  78. get { return this.Errors[0].Procedure;}
  79. }
  80. public string Server {
  81. get { return this.Errors[0].Server;}
  82. }
  83. public byte State {
  84. get { return this.Errors[0].State;}
  85. }
  86. override public string Source {
  87. get { return this.Errors[0].Source;}
  88. }
  89. public override string ToString() {
  90. StringBuilder sb = new StringBuilder(base.ToString());
  91. sb.AppendLine();
  92. sb.AppendFormat(SQLMessage.ExClientConnectionId(), _clientConnectionId);
  93. // Append the error number, state and class if the server provided it
  94. if (Number != 0) {
  95. sb.AppendLine();
  96. sb.AppendFormat(SQLMessage.ExErrorNumberStateClass(), Number, State, Class);
  97. }
  98. // If routed, include the original client connection id
  99. if (Data.Contains(OriginalClientConnectionIdKey)) {
  100. sb.AppendLine();
  101. sb.AppendFormat(SQLMessage.ExOriginalClientConnectionId(), Data[OriginalClientConnectionIdKey]);
  102. }
  103. // If routed, provide the routing destination
  104. if (Data.Contains(RoutingDestinationKey)) {
  105. sb.AppendLine();
  106. sb.AppendFormat(SQLMessage.ExRoutingDestination(), Data[RoutingDestinationKey]);
  107. }
  108. return sb.ToString();
  109. }
  110. static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion) {
  111. return CreateException(errorCollection, serverVersion, Guid.Empty);
  112. }
  113. static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, SqlInternalConnectionTds internalConnection, Exception innerException = null) {
  114. Guid connectionId = (internalConnection == null) ? Guid.Empty : internalConnection._clientConnectionId;
  115. var exception = CreateException(errorCollection, serverVersion, connectionId, innerException);
  116. if (internalConnection != null) {
  117. if ((internalConnection.OriginalClientConnectionId != Guid.Empty) && (internalConnection.OriginalClientConnectionId != internalConnection.ClientConnectionId)) {
  118. exception.Data.Add(OriginalClientConnectionIdKey, internalConnection.OriginalClientConnectionId);
  119. }
  120. if (!string.IsNullOrEmpty(internalConnection.RoutingDestination)) {
  121. exception.Data.Add(RoutingDestinationKey, internalConnection.RoutingDestination);
  122. }
  123. }
  124. return exception;
  125. }
  126. static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, Guid conId, Exception innerException = null) {
  127. Debug.Assert(null != errorCollection && errorCollection.Count > 0, "no errorCollection?");
  128. // concat all messages together MDAC 65533
  129. StringBuilder message = new StringBuilder();
  130. for (int i = 0; i < errorCollection.Count; i++) {
  131. if (i > 0) {
  132. message.Append(Environment.NewLine);
  133. }
  134. message.Append(errorCollection[i].Message);
  135. }
  136. if (innerException == null && errorCollection[0].Win32ErrorCode != 0 && errorCollection[0].Win32ErrorCode != -1) {
  137. innerException = new Win32Exception(errorCollection[0].Win32ErrorCode);
  138. }
  139. SqlException exception = new SqlException(message.ToString(), errorCollection, innerException, conId);
  140. exception.Data.Add("HelpLink.ProdName", "Microsoft SQL Server");
  141. if (!ADP.IsEmpty(serverVersion)) {
  142. exception.Data.Add("HelpLink.ProdVer", serverVersion);
  143. }
  144. exception.Data.Add("HelpLink.EvtSrc", "MSSQLServer");
  145. exception.Data.Add("HelpLink.EvtID", errorCollection[0].Number.ToString(CultureInfo.InvariantCulture));
  146. exception.Data.Add("HelpLink.BaseHelpUrl", "http://go.microsoft.com/fwlink");
  147. exception.Data.Add("HelpLink.LinkId", "20476");
  148. return exception;
  149. }
  150. internal SqlException InternalClone() {
  151. SqlException exception = new SqlException(Message, _errors, InnerException, _clientConnectionId);
  152. if (this.Data != null)
  153. foreach (DictionaryEntry entry in this.Data)
  154. exception.Data.Add(entry.Key, entry.Value);
  155. exception._doNotReconnect = this._doNotReconnect;
  156. return exception;
  157. }
  158. // Do not serialize this field! It is used to indicate that no reconnection attempts are required
  159. internal bool _doNotReconnect = false;
  160. }
  161. }