在SqlBulkCopy写入服务器新行后,DB触发器将不会执行

时间:2016-04-20 19:48:45

标签: c# sql sql-server sqlbulkcopy

我在插入新行后尝试更新ssn列。指定我需要将null设置为ssn列并将该加密值设置为另一列。我的触发器有什么错误或有什么问题吗?

ALTER TRIGGER [dbo].[MemberInsert]
ON [dbo].[Member]
AFTER INSERT
AS 
BEGIN   
SET NOCOUNT ON;

OPEN SYMMETRIC KEY SSN_Key
    DECRYPTION BY CERTIFICATE MyCer;

UPDATE Member
SET  [EncryptedSSN] = EncryptByKey(Key_GUID('SSN_Key'), inserted.SSN)
FROM inserted
INNER JOIN dbo.Member On inserted.MemberId = Member.MemberId

UPDATE Member
SET [SSN] = null 
FROM inserted
INNER JOIN dbo.Member On inserted.MemberId = Member.MemberId
END

现在添加行没有任何结果。此外,我必须添加使用SqlBulkCopy插入新行。触发器以这种方式捕获事件是否有问题?

1 个答案:

答案 0 :(得分:1)

默认情况下,SqlBulkCopy不会检查触发器,您必须指定选项:SqlBulkCopyOptions

var options = SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.CheckConstraints;
using (var bulkCopy = new SqlBulkCopy(connection, options, transaction))
{
    // ...Code...
}