1

我需要将数据批量导入 SQL Server 数据库。

我的 JSON 看起来像这样:

$projectsJSON = @"
[
    {
        "id":  35,
        "created_at":  "2016-01-12T11:40:36+01:00",
        "customer_id":  34,
        "name":  ".com",
        "note":  "COMXXXX-",
        "updated_at":  "2016-07-15T12:13:54+02:00",
        "archived":  false,
        "customer_name":  "PMName"
    },
    {
        "id":  23,
        "created_at":  "2010-01-11T12:58:50+01:00",
        "customer_id":  43,
        "name":  "PN",
        "note":  "{\r\n    \"Billable\": 1\r\n}\r\n",
        "updated_at":  "2017-11-24T15:49:31+01:00",
        "archived":  false,
        "customer_name":  "MSM"
    }
]
"@

$projects = $projectsJSON |ConvertFrom-Json
$dt2 = New-Object system.Data.DataTable
$dt2 = $projects|select-object id, created_at, customer_id, name, note, updated_at, archived, customer_name

$bulkCopy = New-Object Data.SqlClient.SqlBulkCopy($DestConnStr, [System.Data.SqlClient.SqlBulkCopyOptions]::KeepIdentity)
$bulkCopy.BulkCopyTimeout = 600
$bulkCopy.DestinationTableName = "project"
$bulkCopy.WriteToServer($dt2)

不幸的是,我不断收到此错误:

无法将参数“rows”,值为:“System.Object[]”,为“WriteToServer”键入“System.Data.DataRow[]”:“无法转换”@{id=35; created_at=2016-01-12T11:40:36+01:00; 客户id=34;名称=.com;注意=COMXXXX-; 更新时间=2016-07-15T12:13:54+02:00;存档=假;customer_name=PMName}”类型“Selected.System.Management.Automation.PSCustomObject”的值以键入“System.Data.DataRow”。在 P:\PsideSync\sync.ps1:261 char:3 + $bulkCopy.WriteToServer($dt2) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

将大量 JSON 数据导入 SQL Server 的正确方法是什么?

TIA

4

1 回答 1

4

正如@IRon 所指出的,您正在填充 $dt2 两次。我假设您想进行第二次初始化。

您可以从这里获取 Out-DataTable:https ://gallery.technet.microsoft.com/scriptcenter/4208a159-a52e-4b99-83d4-8048468d29dd

那么这只是一个调用的问题

$projects = $projectsJSON |ConvertFrom-Json
$dt2 = $projects|select-object id, created_at, customer_id, name, note, updated_at, archived, customer_name

$bulkCopy = New-Object Data.SqlClient.SqlBulkCopy($DestConnStr, [System.Data.SqlClient.SqlBulkCopyOptions]::KeepIdentity)
$bulkCopy.BulkCopyTimeout = 600
$bulkCopy.DestinationTableName = "project"
$bulkCopy.WriteToServer($dt2 | Out-DataTable)

免责声明:我自己没有尝试过。

于 2017-12-07T14:25:56.747 回答