将数据集加载到 SQL 脚本(在 powershell 中)
我想从一台服务器上的 SQL Server 数据库中提取大型数据集,然后将该数据集包含在另一台服务器上的另一个数据库中。由于链接服务器不是一个选项,我尝试了 powershell 脚本,并且想要类似的内容:
$connection = New-Object System.Data.SqlClient.SqlConnection $command = New-Object System.Data.SqlClient.SqlCommand ... etc for SqlDataAdapter as well $adapter.Fill($dataset1) $connection2 = ... (to a database on another server) $command2 = ... Load $dataset1 into $command2 $command2.Fill($dataset2)
如何将数据集 1 加载到命令 2 中?
I would like to extract a large dataset from an SQL server database on one server and then include that dataset on another database on a different server. As link server is not an option, I have tried a powershell script, and would like something like:
$connection = New-Object System.Data.SqlClient.SqlConnection $command = New-Object System.Data.SqlClient.SqlCommand ... etc for SqlDataAdapter as well $adapter.Fill($dataset1) $connection2 = ... (to a database on another server) $command2 = ... Load $dataset1 into $command2 $command2.Fill($dataset2)
How do I load dataset 1 into command 2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定要将整个数据集保存在内存中吗?
如果您只想将查询结果复制到另一个表,最好使用数据读取器以避免内存不足异常。
编辑:
阅读 Mike 的评论后,PowerShell 可能会展开 datareader 对象,我重试了代码替换。
它
仍然有效,并且我没有出现内存不足的异常,直到
我观察到问题,我没有理由尝试迈克的变体。
第二次编辑:
添加来修改代码,
我通过在之前和之后
但我没有观察到内存使用情况有任何变化,因此我得出结论,Mike 关于 PowerShell 展开 datareader 对象的假设不适用。
Are you sure, that you want to have the whole dataset in memory ?
If you just want to copy the result of a query to another table better use data reader to avoid out of memory exceptions.
Edit:
After reading Mikes comment, that PowerShell possibly unrolls the datareader object, I retried my code replacing.
by
It still worked and I had no out of memory exception in
Until I observe problems, I have no reason to try Mikes variation.
2nd Edit:
I modified the code by adding
before and after
I do not observe any changesin memory usage and therefore I conclude that Mikes assumption about PowerShell unrolling the datareader object doesn't apply.
您是否考虑过 SQLBulkCopy 类? 这里是一个 powershell 函数,它使用它将数据从一个数据集推送到另一个 SQL 连接。
Have you thought about the SQLBulkCopy class? Here's a powershell function that uses it to push data from one dataset to a different SQL Connection.