【问题标题】:How to add 'Real' type to a DataTable?如何将“Real”类型添加到 DataTable?
【发布时间】:2020-05-08 00:22:40
【问题描述】:

我正在将 csv 文件中的记录批量复制到 sql 表中。 sql 表的列是 varchar,列是真实数据类型(基于我们给定的 csv 属性)

假设前 7 列是 varchar(100) 的外键,其余 80 多列是 Real 数据类型。

在大容量复制期间,我使用了 Out-DataTable 函数,因为显然这是大容量复制最有效的方法(尤其是我们的文件包含 1000 条记录)。

但是,我收到以下错误:

Exception calling "WriteToServer" with "1" argument(s): "The given value of type String from the data source cannot be converted to type real of the specified target column."

现在我希望错误可以准确指定哪一列,但根据我的研究,我发现这可能与 presumed 的数据类型是所有列的字符串类型有关。

使用以下内容进行验证:$column.DataType

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object
True     True     String                                   System.Object

所以问题是:我如何告诉 Datatable 允许前 7 列是字符串,而其余的列是真正的数据类型?

代码如下:

function Get-Type 
{ 
    param($type) 

$types = @( 
'System.Boolean', 
'System.Byte[]', 
'System.Byte', 
'System.Char', 
'System.Datetime', 
'System.Decimal', 
'System.Double', 
'System.Guid', 
'System.Int16', 
'System.Int32', 
'System.Int64', 
'System.Single', 
'System.UInt16', 
'System.UInt32', 
'System.UInt64') 

    if ( $types -contains $type ) { 
        Write-Output "$type" 
    } 
    else { 
        Write-Output 'System.String' 

    } 
} #Get-Type

function Out-DataTable 
{ 
    [CmdletBinding()] 
    param([Parameter(Position=0, Mandatory=$true, ValueFromPipeline = $true)] [PSObject[]]$InputObject) 

    Begin 
    { 
        $dt = new-object Data.datatable   
        $First = $true  
    } 
    Process 
    { 
        foreach ($object in $InputObject) 
        { 
            $DR = $DT.NewRow()   
            foreach($property in $object.PsObject.get_properties()) 
            {   
                if ($first) 
                {   
                    $Col =  new-object Data.DataColumn   
                    $Col.ColumnName = $property.Name.ToString()   
                    if ($property.value) 
                    { 
                        if ($property.value -isnot [System.DBNull]) { 
                            $Col.DataType = [System.Type]::GetType("$(Get-Type $property.TypeNameOfValue)") 
                        } 
                    } 
                    $DT.Columns.Add($Col) 
                }   
                if ($property.Gettype().IsArray) { 
                    $DR.Item($property.Name) =$property.value | ConvertTo-XML -AS String -NoTypeInformation -Depth 1 
                }   
               else { 
                    $DR.Item($property.Name) = $property.value 
                } 
            }   
            $DT.Rows.Add($DR)   
            $First = $false 
        } 
    }  

    End 
    { 
        Write-Output @(,($dt)) 
    } 

} #Out-DataTable

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = $connectionstring
$SqlConnection.Open()

$CSVDataTable = Import-Csv $csvFile | Out-DataTable

# Build the sqlbulkcopy connection, and set the timeout to infinite
$sqlBulkCopy = New-Object ("Data.SqlClient.SqlBulkCopy") -ArgumentList $SqlConnection
$sqlBulkCopy.DestinationTableName = "$schemaName.[$csvFileBaseName]"
$sqlBulkCopy.bulkcopyTimeout = 0
$sqlBulkCopy.batchsize = 50000
$sqlBulkCopy.DestinationTableName = "$schemaName.[$csvFileBaseName]"

#This mapping helps to make sure that the columns match exactly because BulkCopy depends on indexes not column names by default. 
#However, with the DataTable, the correct mappings seems to be already taken care of, but putting this here regardless, because why not?
#Better safe than sorry, right? ;)
#https://stackoverflow.com/a/50995201/8397835
foreach ($column in $CSVDataTable.Columns) { $sqlBulkCopy.ColumnMappings.Add($column.ColumnName, $column.ColumnName) > $null }

$sqlBulkCopy.WriteToServer($CSVDataTable)

# Clean Up
$sqlBulkCopy.Close(); $sqlBulkCopy.Dispose()
$CSVDataTable.Dispose()
# Sometimes the Garbage Collector takes too long to clear the huge datatable.
[System.GC]::Collect()

也许是这样的?

伪代码:

foreach ($column in $CSVDataTable.Columns) { 
    $sqlBulkCopy.ColumnMappings.Add(
        if($DestinationTableName.Column.type -eq 'Real') {
            $column.type() = 'Real'
        }
        $column.ColumnName, $column.ColumnName
    ) > $null 
}

【问题讨论】:

    标签: powershell csv datatable sqlbulkcopy columnmappings


    【解决方案1】:

    Out-DataTable 正在检查第一个输入对象的属性...

    foreach($property in $object.PsObject.get_properties())
    {
        if ($first) 
        {  
    

    ...确定对应DataColumn的DataType...

    if ($property.value -isnot [System.DBNull]) { 
        $Col.DataType = [System.Type]::GetType("$(Get-Type $property.TypeNameOfValue)") 
    } 
    

    问题是,输入对象是由Import-Csv产生的...

    $CSVDataTable = Import-Csv $csvFile | Out-DataTable
    

    ...不会对 CSV 字段进行任何转换;每个属性都是[String] 类型,因此,每个DataColumn 也是。

    The .NET equivalent of real is Single,因此您需要硬编码哪些列(按名称或序号)应为 [Single] 类型...

    $objectProperties = @($object.PSObject.Properties)
    for ($propertyIndex = 0; $propertyIndex -lt $objectProperties.Length)
    {
        $property = $objectProperties[$propertyIndex]
        if ($propertyIndex -lt 7) {
            $columnDataType = [String]
            $itemValue = $property.Value
        }
        else {
            $columnDataType = [Single]
            $itemValue = if ($property.Value -match '^\s*-\s*$') {
                [Single] 0
            } else {
                [Single]::Parse($property.Value, 'Float, AllowThousands, AllowParentheses')
            }
        } 
    
        if ($first) 
        {   
            $Col =  new-object Data.DataColumn   
            $Col.ColumnName = $property.Name
            $Col.DataType = $columnDataType
    
            $DT.Columns.Add($Col) 
        }
    
        $DR.Item($property.Name) = $itemValue
    }
    

    ...或增加您的检测逻辑...

    foreach($property in $object.PSObject.Properties)
    {
        $singleValue = $null
        $isSingle = [Single]::TryParse($property.Value, [ref] $singleValue)
    
        if ($first) 
        {   
            $Col =  new-object Data.DataColumn   
            $Col.ColumnName = $property.Name
            $Col.DataType = if ($isSingle) {
                [Single]
            } else {
                [String]
            }
    
            $DT.Columns.Add($Col) 
        }
    
        $DR.Item($property.Name) = if ($isSingle) {
            $singleValue
        } else {
            $property.value
        }
    }
    

    为了符合DataType 列,此代码在解析成功时将[Single] 值替换为原始属性[String] 值。请注意,我已经删除了对 [DBNull] 和 IsArray 的检查,因为它们永远不会评估为 $true,因为 Import-Csv 只会产生 [String] 属性。

    以上假设如果第一个输入对象的属性值可以解析为[Single],那么对于每个输入对象也是如此。如果不能保证,那么您可以通过所有输入对象来确定适当的列类型,然后通过第二次来加载数据...

    function Out-DataTable
    { 
        End 
        {
            $InputObject = @($input)
            $numberStyle = [System.Globalization.NumberStyles] 'Float, AllowThousands, AllowParentheses'
            $dt = new-object Data.datatable 
    
            foreach ($propertyName in $InputObject[0].PSObject.Properties.Name)
            {
                $columnDataType = [Single]
    
                foreach ($object in $InputObject)
                {
                    $singleValue = $null
                    $propertyValue = $object.$propertyName
                    if ($propertyValue -notmatch '^\s*-?\s*$' `
                        -and -not [Single]::TryParse($propertyValue, $numberStyle, $null, [ref] $singleValue))
                    {
                        # Default to [String] if not all values can be parsed as [Single]
                        $columnDataType = [String]
                        break
                    }
                }
    
                $Col =  new-object Data.DataColumn   
                $Col.ColumnName = $propertyName
                $Col.DataType = $columnDataType
    
                $DT.Columns.Add($Col) 
            }
    
            foreach ($object in $InputObject)
            { 
                $DR = $DT.NewRow()   
                foreach($property in $object.PSObject.Properties) 
                {   
                    $DR.Item($property.Name) = if ($DT.Columns[$property.Name].DataType -eq [Single]) {
                        if ($property.Value -match '^\s*-?\s*$') {
                            [Single] 0
                        } else {
                            [Single]::Parse($property.Value, $numberStyle)
                        }
                    } else {
                        $property.value
                    }
                }   
                $DT.Rows.Add($DR)   
            } 
    
            Write-Output @(,($dt)) 
        }  
    
    } #Out-DataTable
    

    【讨论】:

    • ya 在花了一些时间重新研究之后,似乎 import-csv 使所有内容都成为字符串,无论数据类型如何:/。不幸的是,我不想硬编码,因为该过程应该是动态的,csv 文件可能随时更改,用户无法对其进行硬编码。顺便说一句,我会把这段代码放在哪里?感谢您告诉我关于单人的事情,因为根据文档,我认为等价物是十进制:docs.microsoft.com/en-us/powershell/module/…
    • 最后一个 sn-p 将替换 if ($first) { } 块内的第二个 sn-p。想想看,您还需要用解析的值替换$property.Value,因为我认为DataTable 不会为您转换它,即使它知道数据类型。我认为您将real SQL 数据类型与该文档所指的“真实文字”作为这些数字类型的通用术语混淆了。您可以在这里使用[Single](又名[Float])、[Double] 或[Decimal] 中的任何一个;由于使用了SqlBulkCopy 类,我只是查找了哪一个专门映射到SQL real 类型。
    • 我现在得到这个:Exception setting "Item": "Exception calling "set_Item" with "2" argument(s): "Input string was not in a correct format.Couldn't store <BP19> in Report Year Column. Expected type is Single.""""$DR.Item($property.Name) = $property.value
    • 是的,这就是我提到的缺乏转换。我已经更新了代码来解决这个问题。
    • 我认为 [ref] 是必要的......因为我得到了这个:Argument: '2' should be a System.Management.Automation.PSReference. Use [ref]. $isSingle = [Single]::TryParse($property.Value, $singleVa ...
    【解决方案2】:

    在通过 .readXml 导入 XML 数据后,我遇到了类似的挑战,因为 XML 包含空字符串而不是 dbnull。我进行了很多测试以尽可能快地进行转换,对我来说这是最好的:

    1. 创建一个数据表,将所有列作为数据导入的字符串
    2. 在同一个表中使用正确的目标类型创建名称略有不同的相同列,并创建对字符串列的引用(例如,如果“字符串”类型的第一列名为“c1”,那么我将新列命名为类型为“真实的”“c1_”)
    3. 在步骤 2 中创建每一列的过程中,还要创建一个表达式,如 'IIF(LEN([c1]=0),NULL,[c1])' 来解决“空字符串”困境。
    4. 现在进行批量导入,最后通过 dataTableReader 仅将引用列导出到自己的 dataTable 中。

    【讨论】:

    • 我想到了这个和一个建议,但培根的解决方案效果非常好:)
    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2012-10-25
    • 1970-01-01
    • 2010-09-07
    相关资源
    最近更新 更多