SqlBulkCopy 调用触发器后无法复制数据
运行后没有数据插入数据库表。但是在SQL SERVER中直接批量insert 会触发触发器并插入数据。
--------------------------------------------------------
C#代码如下。
using (SqlTransaction transaction = connsql.BeginTransaction("T_addqqqqq"))
{
using (SqlBulkCopy bulk = new SqlBulkCopy(connsql, SqlBulkCopyOptions.FireTriggers, transaction))
{
bulk.BatchSize = 1000;
bulk.DestinationTableName = tname;
for (int i = 0; i <= jj+1; i++)
{
bulk.ColumnMappings.Add(dt.Columns[i].ToString(), dt.Columns[i].ToString());
}
bulk.WriteToServer(dt);
dt.Clear();
bulk.Close();
}
}
-----------------------------------------------------------------------------------
触发器代码:有重复插入的记录,则删除旧记录(防止重复数据以及更新数据,id 和MAT是主键)
ALTER trigger [dbo].[T_addqqqqq]
on [dbo].[qqqqq]
AFTER INSERT
--instead of INSERT
as
DECLARE @MAT nvarchar(36) ,@id nvarchar(30)
declare @itemnumber int --定义需要循环的次数
declare @tagint int --定义标志字段,用于结束循环
set @tagint=1
declare myCursor1 scroll cursor for select MAT, id from inserted
open myCursor1
select @itemnumber = count(*) from inserted
if(@itemnumber>0)
begin
while @tagint<=@itemnumber
begin
fetch next from myCursor1 into @MAT, @id
IF EXISTS ( SELECT 1 FROM qqqqq where @MAT = MAT)
begin
delete