I have read many posts related to this issue, but couldn't find an answer. I am trying to load a large amount of data from Excel into SQL Server. Thousands of records. And I am getting this exception:
String or binary data would be truncated. The statement has been terminated.
Obviously some values exceed the field size in the database. The error comes from SQL Server AFIK.
My question - How could I possibly know what record and what field value caused this?
There are no specific details in EF exception, except the one I mentioned.
Any help is appreciated.
Some asked for the code fragment, but it's actually very simple, the problem is not with the code:
// employees is a List<Employee> collection loaded from Excel
using (var context = new Entities())
{
employees.ForEach(e => context.Employee.AddObject(e));
context.SaveChanges();
}
Also the suggested approach to use DbEntityValidationException (which is only available in Entity Framework 5.0) is not working, the catch block didn't catch the exception.
try
{
ImportData();
}
catch (DbEntityValidationException ex)
{
foreach (var item in ex.EntityValidationErrors)
{
//...
}
}
The only solution that I found so far is to use SQL Server Profiler, and define the following events to monitor:
Now I can see the Email is too long.
To fix this error, patch to SQL Server 2016 SP2, CU6 or newer (including SQL Server 2017), and then turn on trace flag 460. You can enable it at the query level or at the server level.
How to fix “String or binary data would be truncated” The main reason behind this error is the more amount of data that we are trying to store in a column than a specific column can store. So a quick solution to solve this error is by increase the column size.
The "String or binary data would be truncated" error indicates that the procedure is attempting to store something in the DBServerInfo table that is larger than the column allows. The two known reasons this can occur are: SQL Server has at least one database whose name exceeds 25 characters in length.
Truncating a table is a data layer operation, not an object operation. The equivalent in Entity Framework would be to load all objects from the database and delete them one by one. You don't want that, you want to truncate the table. Then dive down into SQL and truncate that table.
catch (DbEntityValidationException ex)
{
foreach (var item in ex.EntityValidationErrors)
{
//... inspect here
}
}
You can find the information you need inside foreach loop.
Hope that helps.
You can't at that level. SQL Server is rejecting the entire query.
I would add some pre-checks to the data against your database constraints for string size, date formats, etc.
Alternatively you could TRIM
each string field in the raw data to the corresponding field size before trying to insert.
You can check data before saving, using EF metadata, and raise appropriate error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With