SqlBulkInsert - 如何设置火灾触发器,检查约束?

发布于 2024-07-09 17:23:36 字数 807 浏览 5 评论 0原文

我正在使用具有有限权限的数据库用户,使用 ADO.NET 2.0 SqlBulkCopy 对象从 C# 方法执行批量插入到 MS SQL 2005 数据库中。 当我尝试运行该操作时,收到错误消息:

批量复制失败。 用户没有 表的 ALTER TABLE 权限 '桌子'。 改变 需要 TABLE 权限 大容量复制操作的目标表 如果表有触发器或检查 约束,但 'FIRE_TRIGGERS''CHECK_CONSTRAINTS' 批量提示不是 指定为批量复制的选项 命令。

我阅读了一些文档并使用构造函数创建了批量复制对象,该构造函数允许我指定这些内容:

    SqlBulkCopy bc = new SqlBulkCopy(
        System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"],
        SqlBulkCopyOptions.FireTriggers & SqlBulkCopyOptions.CheckConstraints);

但这不会改变任何内容 - 我收到与以前相同的错误消息。 我尝试摆弄其他一些 SqlBulkCopyOptions 值,但没有成功。 我真的认为这可以解决问题,我错过了什么吗?

我在向我的用户授予对表的 ALTER 权限后测试了该过程,并且操作成功。 然而,这不适合我的情况。

I'm performing a bulk insert with an ADO.NET 2.0 SqlBulkCopy object from a C# method into a MS SQL 2005 database, using a database user with limited permissions. When I try to run the operation, I get the error message:

Bulk copy failed. User does not have
ALTER TABLE permission on table
'theTable'. ALTER
TABLE permission is required on the
target table of a bulk copy operation
if the table has triggers or check
constraints, but 'FIRE_TRIGGERS' or
'CHECK_CONSTRAINTS' bulk hints are not
specified as options to the bulk copy
command.

I read some documentation and created the bulk copy object with the constructor that lets me specify such things:

    SqlBulkCopy bc = new SqlBulkCopy(
        System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"],
        SqlBulkCopyOptions.FireTriggers & SqlBulkCopyOptions.CheckConstraints);

But this doesn't change anything - I get the same error message as before. I tried fiddling with some of the other SqlBulkCopyOptions values but no luck. I really thought this would fix the problem, am I missing something?

I tested the procedure after granting ALTER on the table to my user, and the operation succeeded. However this is not an option for my situation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极致的悲 2024-07-16 17:23:36

解决了! 看起来我需要复习一下标志枚举。 当我应该对枚举值进行或运算时,我却对它们进行了按位与运算。

SqlBulkCopyOptions.FireTriggers & SqlBulkCopyOptions.CheckConstraints

计算结果为零(相当于 SqlBulkCopyOptions.Default。)

SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.CheckConstraints

工作正常并允许批量插入完成。

Solved it! Looks like I need a refresher on flags enums. I was bitwise ANDing the enum values when I should have been ORing them.

SqlBulkCopyOptions.FireTriggers & SqlBulkCopyOptions.CheckConstraints

evaluates to zero (which is equivalent to SqlBulkCopyOptions.Default.)

SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.CheckConstraints

Worked correctly and allowed the bulk insert to complete.

我一直都在从未离去 2024-07-16 17:23:36

仅可能,抱歉

BULK INSERT< 的 SQL 文档/a> 指定 3 种需要 ALTER TABLE 的情况。 您列出了其中 2 个。 即使不需要,是否设置了 KeepIdentity 选项?

另一种选择是桌子上的触发器已被禁用,这使问题变得混乱。 使用 ALTER TABLE dbo.SomeTable ENABLE TRIGGER ALL 确保启用。

Possibilities only, I'm sorry

SQL documentation for BULK INSERT specifies 3 cases where ALTER TABLE is needed. You listed 2 of them. Is the KeepIdentity option being set, even if not needed?

Another option is that the trigger on the table is disabled already, confusing the issue. Use ALTER TABLE dbo.SomeTable ENABLE TRIGGER ALL to ensure enabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文