使用“1”参数调用“WriteToServer”的异常

时间:2018-05-02 04:59:38

标签: powershell sqlbulkcopy columnmappings

我有powershell脚本将xml文件中的值插入到sql数据库中。

我面临的问题是我似乎无法将值插入sql数据库,因为错误表明数据源中的字符串值未转换为目标列中的uniqueidentifier类型(基本上是sql数据库中的ScheduleId和AccountingPeriodCase_PK_ID列。)

我尝试在columns.add()期间将[guid]数据类型更改为[uniqueidentifier],但除了它们之外我都错了。

脚本:

$dataSource = $env:COMPUTERNAME;                                       
$database = "DataModel.PAModel" 

$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:\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]);
$dt.Columns.Add("WriteBackStatus",[int]);
$dt.Columns.Add("RetryWriteBack",[int]);
$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]);

$dc = New-Object System.Data.DataColumn;
$dc = $dt.Columns.Add("AccountingPeriodCase_PK_ID",[guid]);

$dc.AllowDBNull.Equals($false);
$dc.Unique.Equals($true);

$dt=$ds.Tables[0];

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

#$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");

#$bcp.ColumnMappings.Count;


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

    "Connection to DB is not open.";

    Exit;

}

$bcp.WriteToServer($dt);        #writing to server 
$bcp.ColumnMappings.Clear();
$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\xAccountingPeriodCases .ps1:96 char:1
+ $bcp.WriteToServer($dt);        #writing to server
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

sql数据库表包含以下列: enter image description here

你能帮我解决一下这个错误吗?

ScheduleId和AccountingPeriodCases_PK_ID的值:

enter image description here

0 个答案:

没有答案