From acb62a9cf29504779c91eb5db96d9bb3ff774e8e Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Tue, 28 Sep 2021 16:53:56 -0700 Subject: [PATCH 1/5] Merge the netfx version of SqlException into netcore --- .../Microsoft/Data/SqlClient/SqlException.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs index 2c50c5a3d7..b9d90dbe1d 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs @@ -21,6 +21,9 @@ public sealed partial class SqlException : System.Data.Common.DbException private const int SqlExceptionHResult = unchecked((int)0x80131904); private SqlErrorCollection _errors; +#if NETFRAMEWORK + [System.Runtime.Serialization.OptionalFieldAttribute(VersionAdded = 4)] +#endif private Guid _clientConnectionId = Guid.Empty; private SqlException(string message, SqlErrorCollection errorCollection, Exception innerException, Guid conId) : base(message, innerException) @@ -32,6 +35,9 @@ private SqlException(string message, SqlErrorCollection errorCollection, Excepti private SqlException(SerializationInfo si, StreamingContext sc) : base(si, sc) { +#if NETFRAMEWORK + _errors = (SqlErrorCollection)si.GetValue("Errors", typeof(SqlErrorCollection)); +#endif HResult = SqlExceptionHResult; foreach (SerializationEntry siEntry in si) { @@ -64,6 +70,9 @@ public override void GetObjectData(SerializationInfo si, StreamingContext contex /// // runtime will call even if private... +#if NETFRAMEWORK + [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] +#endif public SqlErrorCollection Errors { get @@ -85,6 +94,14 @@ public Guid ClientConnectionId } } +#if NETFRAMEWORK + /*virtual protected*/ + private bool ShouldSerializeErrors() + { // MDAC 65548 + return ((null != _errors) && (0 < _errors.Count)); + } +#endif + /// public byte Class { From 699e5e8c3ddd88a08aa097f2c7ed5a7e2f24d303 Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Tue, 28 Sep 2021 17:00:06 -0700 Subject: [PATCH 2/5] Move the SqlException.cs from netcore to shared src and update references in the csprojs --- .../src/Microsoft.Data.SqlClient.csproj | 4 +- .../netfx/src/Microsoft.Data.SqlClient.csproj | 6 +- .../Microsoft/Data/SqlClient/SqlException.cs | 247 ------------------ .../Microsoft/Data/SqlClient/SqlException.cs | 0 4 files changed, 7 insertions(+), 250 deletions(-) delete mode 100644 src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlException.cs rename src/Microsoft.Data.SqlClient/{netcore => }/src/Microsoft/Data/SqlClient/SqlException.cs (100%) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index d918134b15..8a823cbd01 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -220,6 +220,9 @@ Microsoft\Data\SqlClient\SqlCredential.cs + + Microsoft\Data\SqlClient\SqlException.cs + Microsoft\Data\SqlClient\SqlInfoMessageEventHandler.cs @@ -504,7 +507,6 @@ Microsoft\Data\SqlClient\SqlEnclaveSession.cs - diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index f54e7630c6..69f810c554 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -294,6 +294,9 @@ Microsoft\Data\SqlClient\SqlEnclaveSession.cs + + Microsoft\Data\SqlClient\SqlException.cs + Microsoft\Data\SqlClient\SqlInfoMessageEventHandler.cs @@ -430,7 +433,7 @@ - + @@ -472,7 +475,6 @@ - diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlException.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlException.cs deleted file mode 100644 index b4e0c7ea2c..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlException.cs +++ /dev/null @@ -1,247 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Collections; -using System.ComponentModel; -using System.Diagnostics; -using System.Globalization; -using System.Runtime.Serialization; -using System.Text; -using Microsoft.Data.Common; - -namespace Microsoft.Data.SqlClient -{ - /// - [Serializable] - public sealed class SqlException : System.Data.Common.DbException - { - private const string OriginalClientConnectionIdKey = "OriginalClientConnectionId"; - private const string RoutingDestinationKey = "RoutingDestination"; - private const int SqlExceptionHResult = unchecked((int)0x80131904); - - private SqlErrorCollection _errors; - [System.Runtime.Serialization.OptionalFieldAttribute(VersionAdded = 4)] - private Guid _clientConnectionId = Guid.Empty; - - private SqlException(string message, SqlErrorCollection errorCollection, Exception innerException, Guid conId) : base(message, innerException) - { - HResult = SqlExceptionHResult; - _errors = errorCollection; - _clientConnectionId = conId; - } - - // runtime will call even if private... - private SqlException(SerializationInfo si, StreamingContext sc) : base(si, sc) - { - _errors = (SqlErrorCollection)si.GetValue("Errors", typeof(SqlErrorCollection)); - HResult = SqlExceptionHResult; - foreach (SerializationEntry siEntry in si) - { - if (nameof(ClientConnectionId) == siEntry.Name) - { - _clientConnectionId = (Guid)si.GetValue(nameof(ClientConnectionId), typeof(Guid)); - break; - } - } - } - - /// - override public void GetObjectData(SerializationInfo si, StreamingContext context) - { - base.GetObjectData(si, context); - si.AddValue("Errors", null); // Not specifying type to enable serialization of null value of non-serializable type - si.AddValue("ClientConnectionId", _clientConnectionId, typeof(object)); - - // Writing sqlerrors to base exception data table - for (int i = 0; i < Errors.Count; i++) - { - string key = "SqlError " + (i + 1); - if (Data.Contains(key)) - { - Data.Remove(key); - } - Data.Add(key, Errors[i].ToString()); - } - } - - /// - [ - DesignerSerializationVisibility(DesignerSerializationVisibility.Content) - ] - public SqlErrorCollection Errors - { - get - { - if (_errors == null) - { - _errors = new SqlErrorCollection(); - } - return _errors; - } - } - - /// - public Guid ClientConnectionId - { - get - { - return this._clientConnectionId; - } - } - - /*virtual protected*/ - private bool ShouldSerializeErrors() - { // MDAC 65548 - return ((null != _errors) && (0 < _errors.Count)); - } - - /// - public byte Class - { - get { return Errors.Count > 0 ? Errors[0].Class : default; } - } - - /// - public int LineNumber - { - get { return Errors.Count > 0 ? Errors[0].LineNumber : default; } - } - - /// - public int Number - { - get { return Errors.Count > 0 ? Errors[0].Number : default; } - } - - /// - public string Procedure - { - get { return Errors.Count > 0 ? Errors[0].Procedure : default; } - } - - /// - public string Server - { - get { return Errors.Count > 0 ? Errors[0].Server : default; } - } - - /// - public byte State - { - get { return Errors.Count > 0 ? Errors[0].State : default; } - } - - /// - override public string Source - { - get { return TdsEnums.SQL_PROVIDER_NAME; } - } - - /// - public override string ToString() - { - StringBuilder sb = new StringBuilder(base.ToString()); - sb.AppendLine(); - sb.AppendFormat(SQLMessage.ExClientConnectionId(), _clientConnectionId); - - // Append the error number, state and class if the server provided it - if (Errors.Count > 0 && Number != 0) - { - sb.AppendLine(); - sb.AppendFormat(SQLMessage.ExErrorNumberStateClass(), Number, State, Class); - } - - // If routed, include the original client connection id - if (Data.Contains(OriginalClientConnectionIdKey)) - { - sb.AppendLine(); - sb.AppendFormat(SQLMessage.ExOriginalClientConnectionId(), Data[OriginalClientConnectionIdKey]); - } - - // If routed, provide the routing destination - if (Data.Contains(RoutingDestinationKey)) - { - sb.AppendLine(); - sb.AppendFormat(SQLMessage.ExRoutingDestination(), Data[RoutingDestinationKey]); - } - - return sb.ToString(); - } - - internal static SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion) - { - return CreateException(errorCollection, serverVersion, Guid.Empty); - } - - internal static SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, SqlInternalConnectionTds internalConnection, Exception innerException = null) - { - Guid connectionId = (internalConnection == null) ? Guid.Empty : internalConnection._clientConnectionId; - var exception = CreateException(errorCollection, serverVersion, connectionId, innerException); - - if (internalConnection != null) - { - if ((internalConnection.OriginalClientConnectionId != Guid.Empty) && (internalConnection.OriginalClientConnectionId != internalConnection.ClientConnectionId)) - { - exception.Data.Add(OriginalClientConnectionIdKey, internalConnection.OriginalClientConnectionId); - } - - if (!string.IsNullOrEmpty(internalConnection.RoutingDestination)) - { - exception.Data.Add(RoutingDestinationKey, internalConnection.RoutingDestination); - } - } - - return exception; - } - - internal static SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, Guid conId, Exception innerException = null) - { - Debug.Assert(null != errorCollection && errorCollection.Count > 0, "no errorCollection?"); - - StringBuilder message = new StringBuilder(); - for (int i = 0; i < errorCollection.Count; i++) - { - if (i > 0) - { - message.Append(Environment.NewLine); - } - message.Append(errorCollection[i].Message); - } - - if (innerException == null && errorCollection[0].Win32ErrorCode != 0 && errorCollection[0].Win32ErrorCode != -1) - { - innerException = new Win32Exception(errorCollection[0].Win32ErrorCode); - } - - SqlException exception = new SqlException(message.ToString(), errorCollection, innerException, conId); - - exception.Data.Add("HelpLink.ProdName", "Microsoft SQL Server"); - - if (!string.IsNullOrEmpty(serverVersion)) - { - exception.Data.Add("HelpLink.ProdVer", serverVersion); - } - exception.Data.Add("HelpLink.EvtSrc", "MSSQLServer"); - exception.Data.Add("HelpLink.EvtID", errorCollection[0].Number.ToString(CultureInfo.InvariantCulture)); - exception.Data.Add("HelpLink.BaseHelpUrl", "http://go.microsoft.com/fwlink"); - exception.Data.Add("HelpLink.LinkId", "20476"); - - return exception; - } - - internal SqlException InternalClone() - { - SqlException exception = new SqlException(Message, _errors, InnerException, _clientConnectionId); - if (this.Data != null) - foreach (DictionaryEntry entry in this.Data) - exception.Data.Add(entry.Key, entry.Value); - exception._doNotReconnect = this._doNotReconnect; - return exception; - } - - // Do not serialize this field! It is used to indicate that no reconnection attempts are required - internal bool _doNotReconnect = false; - } -} diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs similarity index 100% rename from src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs From 3e1a6197ae44e64e93efc00c62efac77ed46cbfd Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Tue, 28 Sep 2021 17:01:38 -0700 Subject: [PATCH 3/5] Fix compiler error due to the relative path change to shared src for the documentation --- .../Microsoft/Data/SqlClient/SqlException.cs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs index b9d90dbe1d..38a6777986 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs @@ -12,7 +12,7 @@ namespace Microsoft.Data.SqlClient { - /// + /// [Serializable] public sealed partial class SqlException : System.Data.Common.DbException { @@ -49,7 +49,7 @@ private SqlException(SerializationInfo si, StreamingContext sc) : base(si, sc) } } - /// + /// public override void GetObjectData(SerializationInfo si, StreamingContext context) { base.GetObjectData(si, context); @@ -68,7 +68,7 @@ public override void GetObjectData(SerializationInfo si, StreamingContext contex } } - /// + /// // runtime will call even if private... #if NETFRAMEWORK [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] @@ -85,7 +85,7 @@ public SqlErrorCollection Errors } } - /// + /// public Guid ClientConnectionId { get @@ -102,49 +102,49 @@ private bool ShouldSerializeErrors() } #endif - /// + /// public byte Class { get { return Errors.Count > 0 ? Errors[0].Class : default; } } - /// + /// public int LineNumber { get { return Errors.Count > 0 ? Errors[0].LineNumber : default; } } - /// + /// public int Number { get { return Errors.Count > 0 ? Errors[0].Number : default; } } - /// + /// public string Procedure { get { return Errors.Count > 0 ? Errors[0].Procedure : default; } } - /// + /// public string Server { get { return Errors.Count > 0 ? Errors[0].Server : default; } } - /// + /// public byte State { get { return Errors.Count > 0 ? Errors[0].State : default; } } - /// + /// override public string Source { get { return TdsEnums.SQL_PROVIDER_NAME; } } - /// + /// public override string ToString() { StringBuilder sb = new StringBuilder(base.ToString()); From bc66bd1e45d773f4163fb6cab3330241b23ea9e8 Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Tue, 28 Sep 2021 17:07:29 -0700 Subject: [PATCH 4/5] Update file to conform with coding style cleaning up IDE0090, IDE0003 and IDE0008 --- .../Microsoft/Data/SqlClient/SqlException.cs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs index 38a6777986..4877d19930 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs @@ -147,7 +147,7 @@ override public string Source /// public override string ToString() { - StringBuilder sb = new StringBuilder(base.ToString()); + StringBuilder sb = new(base.ToString()); sb.AppendLine(); sb.AppendFormat(SQLMessage.ExClientConnectionId(), _clientConnectionId); @@ -183,7 +183,7 @@ internal static SqlException CreateException(SqlErrorCollection errorCollection, internal static SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, SqlInternalConnectionTds internalConnection, Exception innerException = null) { Guid connectionId = (internalConnection == null) ? Guid.Empty : internalConnection._clientConnectionId; - var exception = CreateException(errorCollection, serverVersion, connectionId, innerException); + SqlException exception = CreateException(errorCollection, serverVersion, connectionId, innerException); if (internalConnection != null) { @@ -205,7 +205,7 @@ internal static SqlException CreateException(SqlErrorCollection errorCollection, { Debug.Assert(null != errorCollection && errorCollection.Count > 0, "no errorCollection?"); - StringBuilder message = new StringBuilder(); + StringBuilder message = new(); for (int i = 0; i < errorCollection.Count; i++) { if (i > 0) @@ -220,7 +220,7 @@ internal static SqlException CreateException(SqlErrorCollection errorCollection, innerException = new Win32Exception(errorCollection[0].Win32ErrorCode); } - SqlException exception = new SqlException(message.ToString(), errorCollection, innerException, conId); + SqlException exception = new(message.ToString(), errorCollection, innerException, conId); exception.Data.Add("HelpLink.ProdName", "Microsoft SQL Server"); @@ -238,11 +238,16 @@ internal static SqlException CreateException(SqlErrorCollection errorCollection, internal SqlException InternalClone() { - SqlException exception = new SqlException(Message, _errors, InnerException, _clientConnectionId); - if (this.Data != null) - foreach (DictionaryEntry entry in this.Data) + SqlException exception = new(Message, _errors, InnerException, _clientConnectionId); + if (Data != null) + { + foreach (DictionaryEntry entry in Data) + { exception.Data.Add(entry.Key, entry.Value); - exception._doNotReconnect = this._doNotReconnect; + } + } + + exception._doNotReconnect = _doNotReconnect; return exception; } From d596eddae9a5f4118019542d138aa2030416f2a4 Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Wed, 29 Sep 2021 15:55:23 -0700 Subject: [PATCH 5/5] Remove unreferenced method and update the getters with expression bodies --- .../Microsoft/Data/SqlClient/SqlException.cs | 65 +++---------------- 1 file changed, 10 insertions(+), 55 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs index 4877d19930..bbc8670aa8 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs @@ -20,7 +20,7 @@ public sealed partial class SqlException : System.Data.Common.DbException private const string RoutingDestinationKey = "RoutingDestination"; private const int SqlExceptionHResult = unchecked((int)0x80131904); - private SqlErrorCollection _errors; + private readonly SqlErrorCollection _errors; #if NETFRAMEWORK [System.Runtime.Serialization.OptionalFieldAttribute(VersionAdded = 4)] #endif @@ -73,76 +73,31 @@ public override void GetObjectData(SerializationInfo si, StreamingContext contex #if NETFRAMEWORK [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] #endif - public SqlErrorCollection Errors - { - get - { - if (_errors == null) - { - _errors = new SqlErrorCollection(); - } - return _errors; - } - } + public SqlErrorCollection Errors => _errors ?? new SqlErrorCollection(); /// - public Guid ClientConnectionId - { - get - { - return _clientConnectionId; - } - } - -#if NETFRAMEWORK - /*virtual protected*/ - private bool ShouldSerializeErrors() - { // MDAC 65548 - return ((null != _errors) && (0 < _errors.Count)); - } -#endif + public Guid ClientConnectionId => _clientConnectionId; /// - public byte Class - { - get { return Errors.Count > 0 ? Errors[0].Class : default; } - } + public byte Class => Errors.Count > 0 ? Errors[0].Class : default; /// - public int LineNumber - { - get { return Errors.Count > 0 ? Errors[0].LineNumber : default; } - } + public int LineNumber => Errors.Count > 0 ? Errors[0].LineNumber : default; /// - public int Number - { - get { return Errors.Count > 0 ? Errors[0].Number : default; } - } + public int Number => Errors.Count > 0 ? Errors[0].Number : default; /// - public string Procedure - { - get { return Errors.Count > 0 ? Errors[0].Procedure : default; } - } + public string Procedure => Errors.Count > 0 ? Errors[0].Procedure : default; /// - public string Server - { - get { return Errors.Count > 0 ? Errors[0].Server : default; } - } + public string Server => Errors.Count > 0 ? Errors[0].Server : default; /// - public byte State - { - get { return Errors.Count > 0 ? Errors[0].State : default; } - } + public byte State => Errors.Count > 0 ? Errors[0].State : default; /// - override public string Source - { - get { return TdsEnums.SQL_PROVIDER_NAME; } - } + override public string Source => TdsEnums.SQL_PROVIDER_NAME; /// public override string ToString()