Skip to content

Commit

Permalink
Improve truncation error message (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
Suchiman authored and cheenamalhotra committed Oct 22, 2019
1 parent bd60c01 commit beaab34
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1518,10 +1518,21 @@ private object ConvertValue(object value, _SqlMetaData metadata, bool isNull, re
value = SqlParameter.CoerceValue(value, mt, out coercedToDataFeed, out typeChanged, false);
if (!coercedToDataFeed)
{ // We do not need to test for TextDataFeed as it is only assigned to (N)VARCHAR(MAX)
int len = ((isSqlType) && (!typeChanged)) ? ((SqlString)value).Value.Length : ((string)value).Length;
if (len > length / 2)
string str = ((isSqlType) && (!typeChanged)) ? ((SqlString)value).Value : ((string)value);
int maxStringLength = length / 2;
if (str.Length > maxStringLength)
{
throw SQL.BulkLoadStringTooLong();
if (metadata.isEncrypted)
{
str = "<encrypted>";
}
else
{
// We truncate to at most 100 characters to match SQL Servers behavior as described in
// https://blogs.msdn.microsoft.com/sql_server_team/string-or-binary-data-would-be-truncated-replacing-the-infamous-error-8152/
str = str.Remove(Math.Min(maxStringLength, 100));
}
throw SQL.BulkLoadStringTooLong(_destinationTableName, metadata.column, str);
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,9 @@ internal static Exception BulkLoadNonMatchingColumnName(string columnName, Excep
{
return ADP.InvalidOperation(System.SRHelper.GetString(SR.SQL_BulkLoadNonMatchingColumnName, columnName), e);
}
internal static Exception BulkLoadStringTooLong()
internal static Exception BulkLoadStringTooLong(string tableName, string columnName, string truncatedValue)
{
return ADP.InvalidOperation(System.SRHelper.GetString(SR.SQL_BulkLoadStringTooLong));
return ADP.InvalidOperation(System.SRHelper.GetString(SR.SQL_BulkLoadStringTooLong, tableName, columnName, truncatedValue));
}
internal static Exception BulkLoadInvalidVariantValue()
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Microsoft.Data.SqlClient/netcore/src/Resources/SR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@
<value>The given ColumnName '{0}' does not match up with any column in data source.</value>
</data>
<data name="SQL_BulkLoadStringTooLong" xml:space="preserve">
<value>String or binary data would be truncated.</value>
<value>String or binary data would be truncated in table '{0}', column '{1}'. Truncated value: '{2}'.</value>
</data>
<data name="SQL_BulkLoadInvalidTimeout" xml:space="preserve">
<value>Timeout Value '{0}' is less than 0.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1669,11 +1669,22 @@ private object ConvertValue(object value, _SqlMetaData metadata, bool isNull, re
mt = MetaType.GetMetaTypeFromSqlDbType(type.SqlDbType, false);
value = SqlParameter.CoerceValue(value, mt, out coercedToDataFeed, out typeChanged, false);
if (!coercedToDataFeed)
{ // We do not need to test for TextDataFeed as it is only assigned to (N)VARCHAR(MAX)
int len = ((isSqlType) && (!typeChanged)) ? ((SqlString)value).Value.Length : ((string)value).Length;
if (len > length / 2)
{ // We do not need to test for TextDataFeed as it is only assigned to (N)VARCHAR(MAX)
string str = ((isSqlType) && (!typeChanged)) ? ((SqlString)value).Value : ((string)value);
int maxStringLength = length / 2;
if (str.Length > maxStringLength)
{
throw SQL.BulkLoadStringTooLong();
if (metadata.isEncrypted)
{
str = "<encrypted>";
}
else
{
// We truncate to at most 100 characters to match SQL Servers behavior as described in
// https://blogs.msdn.microsoft.com/sql_server_team/string-or-binary-data-would-be-truncated-replacing-the-infamous-error-8152/
str = str.Remove(Math.Min(maxStringLength, 100));
}
throw SQL.BulkLoadStringTooLong(_destinationTableName, metadata.column, str);
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,9 +975,9 @@ static internal Exception BulkLoadNonMatchingColumnName(string columnName, Excep
{
return ADP.InvalidOperation(StringsHelper.GetString(Strings.SQL_BulkLoadNonMatchingColumnName, columnName), e);
}
static internal Exception BulkLoadStringTooLong()
static internal Exception BulkLoadStringTooLong(string tableName, string columnName, string truncatedValue)
{
return ADP.InvalidOperation(StringsHelper.GetString(Strings.SQL_BulkLoadStringTooLong));
return ADP.InvalidOperation(StringsHelper.GetString(Strings.SQL_BulkLoadStringTooLong, tableName, columnName, truncatedValue));
}
static internal Exception BulkLoadInvalidVariantValue()
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2818,7 +2818,7 @@
<value>The given ColumnName '{0}' does not match up with any column in data source.</value>
</data>
<data name="SQL_BulkLoadStringTooLong" xml:space="preserve">
<value>String or binary data would be truncated.</value>
<value>String or binary data would be truncated in table '{0}', column '{1}'. Truncated value: '{2}'.</value>
</data>
<data name="SQL_BulkLoadInvalidTimeout" xml:space="preserve">
<value>Timeout Value '{0}' is less than 0.</value>
Expand Down

0 comments on commit beaab34

Please sign in to comment.