Return Records From SqlBulkCopy using IDataReader

c# entity-framework sqlbulkcopy sql-server


I'm doing a bulk insert/update to my database by making use of the SqlBulkCopy class. After the data has inserted/updated, I want to do more logic with it.

Currently, my IDataReader has a mix of records to be inserted and updated. All actions against the database work correctly. However, after the data is written, the IDataReader object no longer has any rows.

Is there a way to keep the rows, complete with the auto generated Unique Identifier field for the inserted records?

Here is a snippet from my code:

    public IDataReader DoBulkCopy<T>(List<T> DataToInsert, string DestinationTableName)
        var BulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString, SqlBulkCopyOptions.KeepNulls);
        BulkCopy.BulkCopyTimeout = 0;
        BulkCopy.BatchSize = 50;
        BulkCopy.DestinationTableName = DestinationTableName;

        IDataReader reader = DataToInsert.AsDataReader();

        return reader;

Thanks in advance.

12/8/2012 3:08:43 AM

Accepted Answer

I have come to the conclusion after much research that this is not possible. I was able to come up with a work around. What I did was wrote the DateTime.Now() to a variable prior to executing the query. After the query executed, I did a select query for all records where the LastUpdated timestamp field as greater than the DateTime variable recorded before the insert/update.

Please note this works fine in my scenario because the data in this table is inserted/updated by this scheduler only. If this was an app where the data can come from multiple places or users, this approach would need to be rethought or adjusted.

12/11/2012 4:26:37 AM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow