Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve truncation error message #258

Merged
merged 6 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
}
}
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