如何在不定义目标表的情况下使用 BulkCopy

发布于 2024-12-27 14:08:35 字数 632 浏览 0 评论 0原文

所有,

我有以下 BulkCopy 操作:

// Convert the FlexGrid to a DataTable.
DataTable currData = (DataTable)c1ErrFlexGrid.DataSource;

// Insert the data into the database.
SqlBulkCopy SqlBulkIns = new SqlBulkCopy(strConnString, SqlBulkCopyOptions.Default);
SqlBulkIns.BatchSize = 5000;
SqlBulkIns.DestinationTableName = String.Format("dbo.{0}", strTableName);
SqlBulkIns.WriteToServer(currData);

其中 strConnectionString 是定义的有效连接字符串。过去,当表 strTableName 存在并且定义了字段时,这种方法效果很好。我现在希望对运行时定义的 DataTable 执行此 BulkCopy 操作;即无需在SQL中预先定义表结构。这可能吗?如果是这样,怎么办?

感谢您抽出时间。

All,

I have the following BulkCopy operation:

// Convert the FlexGrid to a DataTable.
DataTable currData = (DataTable)c1ErrFlexGrid.DataSource;

// Insert the data into the database.
SqlBulkCopy SqlBulkIns = new SqlBulkCopy(strConnString, SqlBulkCopyOptions.Default);
SqlBulkIns.BatchSize = 5000;
SqlBulkIns.DestinationTableName = String.Format("dbo.{0}", strTableName);
SqlBulkIns.WriteToServer(currData);

where strConnectionString is a defined and valid connection string. This has worked fine in the past when the table strTableName exists and the fields are defined. I now wish to perform this BulkCopy operation on a DataTable that is defined at run-time; that is, without predefining the table structure in SQL. Is this possible? If so, how?

Thanks for your time.

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

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

发布评论

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

评论(1

你怎么敢 2025-01-03 14:08:35

您可以使用此 SqlTableCreator 对象从数据表创建 SQL 表,然后运行批量插入。

You could use this SqlTableCreator object to create a SQL table from your datatable and then run the bulk insert.

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