I am trying to do a bulk insert of a List of object (List invoices). Sometimes it fails and throws an exception. However, I want to know which rows failed. This way I can redo the bulk insert by omitting those rows. Can I do this?
Find Failed row/rows on SqlBulkCopy.WriteToServer exception and retrySqlBulkCopy by omitting those rows that failed
2.4k Views Asked by user1424876 At
1
There are 1 best solutions below
Related Questions in C#
- Passing arguments to main in C using Eclipse
- kernel module does not print packet info
- error C2016 (C requires that a struct or union has at least one member) and structs typedefs
- Drawing with ncurses, sockets and fork
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- Configured TTL for A record(s) backing CNAME records
- Allocating memory for pointers inside structures in functions
- Finding articulation point of undirected graph by DFS
- C first fgets() is being skipped while the second runs
- C std library don't appear to be linked in object file
- gcc static library compilation
- How to do a case-insensitive string comparison?
- C programming: Create and write 2D array of files as function
- How to read a file then store to array and then print?
- Function timeouts in C and thread
Related Questions in SQL-SERVER
- SQL server not returning all rows
- Big data with spatial queries/indexing
- Conditional null constraint on Null
- SQL Query - Order by String (which contains number and chars)
- Optimising a slow running SQL Server Stored procedure ordered by calculated fields to return a closest match
- Dynamics CRM Publishing Customizations - Multi Developers
- Is there anyway to set the relationship of many tables from Model?
- Implementation of Rank and Dense Rank in MySQL
- ORM Code First versa Database First in Production
- MVC : Insert data to two tables
- Data streams in case of Merge
- table with multiple IDs but seperate notes need sorting (Tried SQL code to make a union query)
- SQL table Partitioning by Year with ColumnStore index implemented on the table
- Defining which network to use for SQL Server 2012 Management Studio
- Fill a week days in a table with preceding Sundays value
Related Questions in BULKINSERT
- MSSQL Bulk Insert CSV - Multiple columns include commas
- BULK generate data SQL Server
- PostgreSQL COPY IN and NPGSQL
- Use Bulk Insert or use default data in SQL
- EntityFramework.BulkInsert.SqlServerCe error
- Why BULK INSERT/BCP includes Sort operator although I have done everything to avoid that? (SQL Server 2012)
- The best way to fill table with data using npgsql
- Bulk insert CSV with as last column datatype bit
- Bulk insert .txt file in SQL
- Does MongoDB bulk update work for fields which don't exist
- How to Bulk Insert from XLSX file extension in SQL Server 2012?
- Mongo Bulk Insert across multiple collections
- How to insert bulk data into SQLite database in iOS using swift
- OleDbDataReader - No value given for one or more required parameters
- Elastic Search bulk indexing using perl
Related Questions in SQLBULKCOPY
- BULK generate data SQL Server
- Missing Data after insert into my end sql table using Sql Bulk Copy
- Is there a way to control the memory usage of C# DataTable
- Bind default value if excel sheet column value null in sql bulk copy upload
- Update previous record when i import from excel to sql database
- SQL Server 2012 - Bulk insert error - This operation conflicts with another pending operation on this transaction
- script task bulkcopy from Excel to Sql Server 2008. Missing first row after the header
- Bulk copy from SQL Server CE to SQL Server
- bcp command is replacing empty strings with nulls
- Unable to get updated value from method of other class
- How to use the SQL Server JDBC bulk copy API
- Best practice for transferring a large amount of data. Using Stored Procedure or SQL Bulk Copy
- SqlBulkCopy start index?
- Determine Security Differences Between Two SQL Servers Database Users
- SQL Server Alternative to reseeding identity column
Related Questions in SQLCLIENT
- Fill: SelectCommand.Connection error on derived iDBCommand
- I'm building a C# AWS Lambda package. How do I properly include System.Data.SqlClient.dll?
- Error when running .ExecuteStoreCommand twice in one ActionResult "Parameter already used"
- SQL Server Command Timeout - executable file only
- 16,000 SqlExceptions logged in two minutes from one user
- SqlClient returning strange OOM exception? C# .NET 4
- SQL client on client machine for Desktop application
- Why is this SqlTransaction rolling back upon closing the connection AFTER the transaction has been Committed?
- Find Failed row/rows on SqlBulkCopy.WriteToServer exception and retrySqlBulkCopy by omitting those rows that failed
- NPoco exhausting connection pool on .NET Core 3.1
- Application Insights SDK SQL Dependency Tracking is not working with Microsoft.Data.SqlClient 2.0
- Cannot create and populate list type System.Data.SqlClient.SqlErrorCollection. Path '$values'
- Cannot connect to SQL Server with Windows authentication from C#
- How to update an xml string into a database without changing from double quotes to single quotes?
- Proper way to pass decimal and string as SqlParameter
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
This is one of the draw backs to using Bulk operations, the feedback is an all or none kind of response.
SqlBulkCopyis deliberately designed for use with sanitised dataSo you should first consider how to sanitise your data before you try to copy it, this can take many forms so we can't cover everything in this post.
The most common Constraints that can fail is with null values (in fields that do not support nulls) and foreign keys (either null or not matching). Usually we can pre-validate the bulk data for nulls and keys that do not yet exist, just query your bulk set to find the rows that have null values in columns that do not support nulls. You can also query for any rows where the values in foreign key columns do not yet exist in the target data base.
If you are approaching this from a generic point of view, so you do not know the table schema in advance then the conceptual process that we usually follow in this scenario is to break the bulk set down into smaller chunks and execute those chunks.
In your interface, allow the user to specify the start row and the number of rows to copy, if it works, remove the rows from the source set. If it fails, ask the user to try again.
Your last option is to not do this in bulk at all! You can still use
SqlBulkCopyhowever only send one row at a time, this allows you to handle wach row when it fails.If you were using
SqlBulkCopyfor performance reasons, (there are of course other non-performance reasons to use SqlBulkCopy) then all that performance is lost if you use this method, however if failure has a low frequency then first trying the full bulk operation, then on failure doing it row by row is an option.This article on Code Project Retrieving failed records after an SqlBulkCopy exception explains a solution to assist this but it should be pretty easy for you to come up with your own implementation.
You could combine the two approaches, trying the whole lot first, then on failure splitting the table into a number of sub tables on failure, then continue to recursively try and then split the tables until you reach tables of 1 row. This would be similar to how the user could go through the same process of elimiation manually and would still retain some performance benefits over going row by row from the start, but this is only advisable for large sets that have relatively low failure rates.