带有notifyafter和transactions的sqlbulkcopy

c# c#-4.0 sql sqlbulkcopy

我有以下代码,我试图用它来测试它是否可能有事务和一个用于引发事件的notifyAfter属性(我已经尝试用一个事件代替我自己创建/提升但是它只是在之后被提升所有行都已复制)。以下链接表明它不可能

MSDN

有没有人有这方面的经验?谢谢

            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;
                        }
                }

热门答案

我假设因为事务,处理只发生在事务提交之后,因此在此之后你不会获得事件。




许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因