如何在不定义目标表的情况下使用 BulkCopy
所有,
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用此 SqlTableCreator 对象从数据表创建 SQL 表,然后运行批量插入。
You could use this SqlTableCreator object to create a SQL table from your datatable and then run the bulk insert.