Reputation: 5223
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();
BulkCopy.WriteToServer(reader);
BulkCopy.Close();
return reader;
}
Thanks in advance.
Upvotes: 0
Views: 478
Reputation: 5223
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.
Upvotes: 0