将数据集加载到 SQL 脚本(在 powershell 中)

发布于 2024-10-17 08:09:08 字数 453 浏览 2 评论 0原文

我想从一台服务器上的 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 技术交流群。

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

发布评论

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

评论(2

吖咩 2024-10-24 08:09:08

您确定要将整个数据集保存在内存中吗?

如果您只想将查询结果复制到另一个表,最好使用数据读取器以避免内存不足异常。

# cf. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx

$SourceConnectionString      = "Data Source=localhost;Initial Catalog=source_db;Integrated Security=True"
$DestinationConnectionString = "Data Source=localhost;Initial Catalog=Destination_db;Integrated Security=True"

$tableName = "MyTable"
$sql = "select * FROM $tableName"

$sourceConnection  = New-Object System.Data.SqlClient.SQLConnection($ConnectionString)
$sourceConnection.open()
$commandSourceData  = New-Object system.Data.SqlClient.SqlCommand($sql,$sourceConnection)
#$commandSourceData .CommandTimeout = '300'

ps 'powershell_ise'
$reader = $commandSourceData.ExecuteReader()
ps 'powershell_ise'


try
{
    $bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $DestinationConnectionString
    $bulkCopy.DestinationTableName = $tableName
    $bulkCopy.BatchSize = 5000
    $bulkCopy.BulkCopyTimeout = 0
    $bulkCopy.WriteToServer($reader)
}
catch
{
    $ex = $_.Exception
    Write-Host "Write-DataTable$($connectionName):$ex.Message"
}
finally
{
    $reader.close()
}

编辑:

阅读 Mike 的评论后,PowerShell 可能会展开 datareader 对象,我重试了代码替换。

$sql = "select * FROM $tableName"

$sql = "select * FROM $tableName union all select * FROM $tableName union all select * FROM $tableName union all select * FROM $tableName union all select * FROM $tableName "

仍然有效,并且我没有出现内存不足的异常,直到

$reader = $commandSourceData.ExecuteReader()

我观察到问题,我没有理由尝试迈克的变体。

第二次编辑:

添加来修改代码,

ps 'powershell_ise'

我通过在之前和之后

$reader = $commandSourceData.ExecuteReader()

但我没有观察到内存使用情况有任何变化,因此我得出结论,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.

# cf. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx

$SourceConnectionString      = "Data Source=localhost;Initial Catalog=source_db;Integrated Security=True"
$DestinationConnectionString = "Data Source=localhost;Initial Catalog=Destination_db;Integrated Security=True"

$tableName = "MyTable"
$sql = "select * FROM $tableName"

$sourceConnection  = New-Object System.Data.SqlClient.SQLConnection($ConnectionString)
$sourceConnection.open()
$commandSourceData  = New-Object system.Data.SqlClient.SqlCommand($sql,$sourceConnection)
#$commandSourceData .CommandTimeout = '300'

ps 'powershell_ise'
$reader = $commandSourceData.ExecuteReader()
ps 'powershell_ise'


try
{
    $bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $DestinationConnectionString
    $bulkCopy.DestinationTableName = $tableName
    $bulkCopy.BatchSize = 5000
    $bulkCopy.BulkCopyTimeout = 0
    $bulkCopy.WriteToServer($reader)
}
catch
{
    $ex = $_.Exception
    Write-Host "Write-DataTable$($connectionName):$ex.Message"
}
finally
{
    $reader.close()
}

Edit:

After reading Mikes comment, that PowerShell possibly unrolls the datareader object, I retried my code replacing.

$sql = "select * FROM $tableName"

by

$sql = "select * FROM $tableName union all select * FROM $tableName union all select * FROM $tableName union all select * FROM $tableName union all select * FROM $tableName "

It still worked and I had no out of memory exception in

$reader = $commandSourceData.ExecuteReader()

Until I observe problems, I have no reason to try Mikes variation.

2nd Edit:

I modified the code by adding

ps 'powershell_ise'

before and after

$reader = $commandSourceData.ExecuteReader()

I do not observe any changesin memory usage and therefore I conclude that Mikes assumption about PowerShell unrolling the datareader object doesn't apply.

谁的年少不轻狂 2024-10-24 08:09:08

您是否考虑过 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.

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