sqlbulkcopy with transactions and notifyafter

c# c#-4.0 sql sqlbulkcopy

Question

I have the following code, which im trying to use to test out if its possible to have transactions and a notifyAfter property used to raise an event(i have already tried substituting the event for one i create/raise myself but it only gets raised after all the rows have been copied). The following link suggests that its not possible

MSDN

Has anyone had any experience with this? Thanks

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                {
                        try
                        {
                            using (SqlBulkCopy copy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity |SqlBulkCopyOptions.UseInternalTransaction))

                            {
                                //Column mapping for the required columns.
                                for (int count = 0; count < numberOfColumns; count++)
                                {
                                    copy.ColumnMappings.Add(count, count);
                                }

                                //SQLBulkCopy parameters.
                                copy.DestinationTableName = dataTableName;
                                copy.BatchSize = batchSize;

                                copy.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
                                copy.NotifyAfter = 5;

                                copy.WriteToServer(fullDataTable);
                            }
                        }
                        //Error(s) occured while trying to commit the transaction.
                        catch (InvalidOperationException transactionEx)
                        {
                            //uploadTransaction.Rollback();
                            status = "The current transaction has been rolled back due to an error. \n\r" + transactionEx.Message;
                            MessageBox.Show(status, "Error Message:");
                            alreadyCaught = true;
                            throw;
                        }
                }
1
0
3/15/2012 11:03:05 PM

Popular Answer

I would presume because of the transaction, the processing only occurs after the transaction is committed hence you won't get the event raised until after that.

0
3/15/2012 11:22:50 PM


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