使用 SqlBulkCopy,如何将数据插入到非默认数据库模式的表中?

发布于 2024-10-05 11:35:21 字数 167 浏览 0 评论 0原文

我需要使用 SqlBulkCopy 将数据插入名为 Staging 的架构中的表中。

看来 API 只允许您使用 DestinationTableName 属性设置目标表名称。

我该如何实现这个目标?是否可以?

I need to insert data into a table in a schema named Staging using SqlBulkCopy.

It appears the API only allows you to set the target table name by using the DestinationTableName property.

How do I accomplish this? Is it possible?

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

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

发布评论

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

评论(1

沙与沫 2024-10-12 11:35:21

虽然 API 不提供显式接口来设置目标架构,但您实际上可以将 2 部分或 3 部分限定表名称填充到 DestinationTableName 属性如下:

b.DestinationTableName = string.Format("[{0}].[{1}]", schemaName, tableName);

b.DestinationTableName =
    string.Format("[{0}].[{1}].[{2}]", databaseName, schemaName, tableName);

鉴于 BULK INSERT 始终支持完全限定的表名,这似乎是一个很大的疏忽这些单独的组件从未进入 API。

此外,由于 DestinationTableName 似乎只是简单地输出到 BULK INSERT 语句中,因此该属性可能容易受到 SQL 注入的攻击。因此,如果您在某个时刻从用户那里获取此信息,请确保在运行此操作之前清理输入。

While the API does not provide an explicit interface to set the destination schema, you can actually stuff a 2- or 3-part qualified table name into the DestinationTableName property like so:

b.DestinationTableName = string.Format("[{0}].[{1}]", schemaName, tableName);

or

b.DestinationTableName =
    string.Format("[{0}].[{1}].[{2}]", databaseName, schemaName, tableName);

Given that BULK INSERT has always supported a fully-qualified table name, it seems like a big oversight that those separate components never made it into the API.

Moreover, as it appears that DestinationTableName is simply output into a BULK INSERT statement, this property may be vulnerable to SQL injection. So if you're getting this information from a user at some point, make sure to sanitize the input before running this operation.

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