来自数据源的String类型的给定值无法转换为指定目标列的类型uniqueidentifier

The given value of type String from the data source cannot be converted to type uniqueidentifier of the specified target column

我在这里尝试的是,使用 PowerShell 脚本将数据从 .xml 文件加载到 SQL 服务器数据库。

我的脚本是这样的:

$dataSource = 'dtsrc';                                       
$database = 'tempdb';   #DB

$connection = New-Object System.Data.SqlClient.SqlConnection;  #setting connection
$connection.ConnectionString = "Server=$dataSource;Database=$database;Integrated Security=True;";
$connection.Open();

$command = New-Object System.Data.SqlClient.SqlCommand;
$command.Connection = $connection;

$as = New-Object System.Data.SqlClient.SqlDataAdapter;
$as.SelectCommand = $command;
$filename = 'C:\DCT\XML\apc.xml';            #file to be loaded into sql server database table

$ds = New-Object System.Data.DataSet;
$ds.ReadXml($filename);    #reading from the file -- line gives output InferSchema

$dt = New-Object System.Data.DataTable;

$dt.Columns.Add("StartTime",[datetime]);
$dt.Columns.Add("EndTime",[datetime]);
$dt.Columns.Add("Status",[int]);
$dt.Columns.Add("ProcessedTime",[datetime]);
$dt.Columns.Add("ScheduleId",[guid]);
$dt.Columns.Add("Model",[string]);
$dt.Columns.Add("ModelVersion",[string]);
$dt.Columns.Add("ModelVersionState",[string]);
$dt.Columns.Add("ModifiedTime",[datetime]);
 $dt.Columns.Add("WriteBackLastRunTime",[datetime]);
 $dt.Columns.Add("ModifiedBy",[string]);
 $dt.Columns.Add("ModelType",[int]);
 $dt.Columns.Add("IsTimeRange",[int]);#b
 $dt.Columns.Add("WriteBackStatus",[int]);
 $dt.Columns.Add("RetryWriteBack",[int]);#b
 $dt.Columns.Add("NumOfRetry",[int]);
 $dt.Columns.Add("FrequencyUnits",[int]);
 $dt.Columns.Add("ScheduleType",[int]);
 $dt.Columns.Add("CustomType",[int]);
 $dt.Columns.Add("ShiftStartTime",[datetime]);
 $dt.Columns.Add("StartWeekDay",[int]);
 $dt.Columns.Add("EndWeekDay",[int]);
 $dt.Columns.Add("WritebackProcessedTime",[datetime]);
 $dt.Columns.Add("DiagStatus",[int]);
 $dt.Columns.Add("AccountingPeriodCase_PK_ID",[guid]);

 $dt = $ds.Tables[0];

$bcp = New-Object 'Data.SqlClient.SqlBulkCopy' $connection; #bulkcopy to the destination table.
$bcp.DestinationTableName = 'dbo.tempor';

#$bcp.ColumnMappings.Count;
$bcp.ColumnMappings.Clear();

$bcp.ColumnMappings.Add('StartTime','StartTime');
$bcp.ColumnMappings.Add('EndTime','EndTime');
$bcp.ColumnMappings.Add('Status','Status');
$bcp.ColumnMappings.Add('ProcessedTime','ProcessedTime');
$bcp.ColumnMappings.Add('ScheduleId','ScheduleId');
$bcp.ColumnMappings.Add('Model','Model');
$bcp.ColumnMappings.Add('ModelVersion','ModelVersion');
$bcp.ColumnMappings.Add('ModelVersionState','ModelVersionState');
$bcp.ColumnMappings.Add('ModifiedTime','ModifiedTime');
$bcp.ColumnMappings.Add('WriteBackLastRunTime','WriteBackLastRunTime');
$bcp.ColumnMappings.Add('ModifiedBy','ModifiedBy');
$bcp.ColumnMappings.Add('ModelType','ModelType');
$bcp.ColumnMappings.Add('IsTimeRange','IsTimeRange');
$bcp.ColumnMappings.Add('WriteBackStatus','WriteBackStatus');
$bcp.ColumnMappings.Add('RetryWriteBack','RetryWriteBack');
$bcp.ColumnMappings.Add('NumOfRetry','NumOfRetry');
$bcp.ColumnMappings.Add('FrequencyUnits','FrequencyUnits');
$bcp.ColumnMappings.Add('ScheduleType','ScheduleType');
#$bcp.ColumnMappings.Add('CustomType','CustomType');
#$bcp.ColumnMappings.Add('ShiftStartTime','ShiftStartTime');
#$bcp.ColumnMappings.Add('StartWeekDay','StartWeekDay');
#$bcp.ColumnMappings.Add('EndWeekDay','EndWeekDay');
$bcp.ColumnMappings.Add('WritebackProcessedTime','WritebackProcessedTime');
$bcp.ColumnMappings.Add('DiagStatus','DiagStatus');
$bcp.ColumnMappings.Add('AccountingPeriodCase_PK_ID','AccountingPeriodCase_PK_ID');    

if ($connection.State -ne [Data.ConnectionState]::Open) {

    'Connection to DB is not open.'

    Exit

}

$bcp.WriteToServer($dt);        #writing to server 
$connection.Close();

我遇到的错误是:

Exception calling "WriteToServer" with "1" argument(s): "The given value of type String from the data source cannot be converted to type uniqueidentifier of the specified target column." At C:\Documents\DCT\SavedScripts\XMLtoDB\AccountingPeriodCases\sample.ps1:91 char:1
+ $bcp.WriteToServer($dt);        #writing to server
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

我创建的 table 的列的数据类型为:

问题是,我想我需要转换 ScheduleId 和 AccountingPeriodCase_PK_ID 列下数据 table 中的字符串,因为它们没有转换为 uniqueidentifier。

我试过使用 [System.Guid]::Parse($dt.Columns[4]); [System.Guid]::Parse($dt.Columns[24]);

但错误重复出现。

要在 ScheduleId 和 AccountingPeriodCase_PK_ID 下加载的 xml 内容如下所示:

<ScheduleId>db6f3178-4702-456c-904b-2cd833b54efa</ScheduleId>
<AccountingPeriodCase_PK_ID>9544940d-8d1a-e711-80d3-0050569c2fb6</AccountingPeriodCase_PK_ID>

谁能帮我解决这个问题? 谢谢

从我之前遇到的 scenario 来看,它归结为您输入的来源。

问题是,如果您正在读取 XML 文件,那么您一定会遇到错误,因为在读取 flat-file(txt, csv)XML 时,PowerShell 会将未定义的类型解析为 String。诀窍是对源进行某种控制。如果您的源是 PowerShell 变量,例如 datatablearray,那么您可以直接将此变量的值插入目标 table,因为原始数据类型为table 中的列被保留。

这是我遵循的方法。在我的例子中,源是 Invoke-Sqlcmd 的结果,它保留了原始数据类型,因此减少了插入错误。

再次如 OP 评论中所述,他正在根据 columns 而不是 rows 解析值。这也可能导致错误。例如,还有一种使用 INSERT 语句在 table 中插入值的方法。请注意,SQL Server INSERT 语句在行级别而不是列级别插入值。在这种情况下,明智地解析值 column 可能会失败。