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 1bd80ed583..1fe89fc980 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
@@ -262,6 +262,9 @@
Microsoft\Data\SqlClient\SqlErrorCollection.cs
+
+ Microsoft\Data\SqlClient\SqlException.cs
+
Microsoft\Data\SqlClient\SqlInfoMessageEventHandler.cs
@@ -543,7 +546,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 c88a172035..5c21a6e9f0 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
@@ -345,6 +345,9 @@
Microsoft\Data\SqlClient\SqlErrorCollection.cs
+
+ Microsoft\Data\SqlClient\SqlException.cs
+
Microsoft\Data\SqlClient\SqlInfoMessageEventHandler.cs
@@ -527,7 +530,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 62%
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
index 2c50c5a3d7..bbc8670aa8 100644
--- a/src/Microsoft.Data.SqlClient/netcore/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
{
@@ -20,7 +20,10 @@ 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
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)
{
@@ -43,7 +49,7 @@ private SqlException(SerializationInfo si, StreamingContext sc) : base(si, sc)
}
}
- ///
+ ///
public override void GetObjectData(SerializationInfo si, StreamingContext context)
{
base.GetObjectData(si, context);
@@ -62,75 +68,41 @@ public override void GetObjectData(SerializationInfo si, StreamingContext contex
}
}
- ///
+ ///
// runtime will call even if private...
- public SqlErrorCollection Errors
- {
- get
- {
- if (_errors == null)
- {
- _errors = new SqlErrorCollection();
- }
- return _errors;
- }
- }
+#if NETFRAMEWORK
+ [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
+#endif
+ public SqlErrorCollection Errors => _errors ?? new SqlErrorCollection();
- ///
- public Guid ClientConnectionId
- {
- get
- {
- return _clientConnectionId;
- }
- }
+ ///
+ 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()
{
- StringBuilder sb = new StringBuilder(base.ToString());
+ StringBuilder sb = new(base.ToString());
sb.AppendLine();
sb.AppendFormat(SQLMessage.ExClientConnectionId(), _clientConnectionId);
@@ -166,7 +138,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)
{
@@ -188,7 +160,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)
@@ -203,7 +175,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");
@@ -221,11 +193,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;
}