InsertCommand SQLDataAdapter在Update Method之前抛出异常

时间:2017-07-20 22:58:18

标签: c# powershell ado.net

我正在使用ADO.NET框架使用 Powershell 所以我有一个数据集内部数据集 编辑数据表然后尝试保存我得到以下错误,错误是在此行抛出

$Adapter.Update($Global:dtCalendars)

the exception message is the following:
*Exception calling "Update" with "1" argument(s): "The parameterized query '(@BudgetYear int,@StartDate date,@EndDate date,@IsActive bit,@Mo' 
expects the parameter '@StartDate', which was not supplied." - Line Number: 603
At C:\Users\jabou-ghannam\OneDrive - Island Hospitality\Island Hospitality\PowerShell\PS TFS Projects\Budget Command 
Center\BudgetCommandCenter.ps1:750 char:1
+ $Win.ShowDialog()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException*



Here is my full Powershell script for saving datatable in sql table. 

$dtAdded = $Global:dtCalendars.GetChanges([System.Data.DataRowState]::Added)

$SQLConnection.Open()        
$Adapter = New-Object System.Data.SqlClient.SqlDataAdapter

if($dtAdded -ne $null) 
{
    $InsertQuery = 
@"
    INSERT INTO [Financials].[Calendars]
    ([BudgetYear],[StartDate],[EndDate],[IsActive],[ModifiedBy],[ModifiedOn])
    VALUES(@BudgetYear,@StartDate,@EndDate,@IsActive,@ModifiedBy,@ModifiedOn)
"@                                
    $Adapter.InsertCommand = New-Object System.Data.SqlClient.SqlCommand($InsertQuery,$SQLConnection)
    $Adapter.InsertCommand.CommandType = [System.Data.CommandType]::Text
    $Parameter = $Adapter.InsertCommand.Parameters.Add("@RowCount", [System.Data.SqlDbType]::Int , -1,"@@ROWCOUNT")
    $Parameter.Direction = [System.Data.ParameterDirection]::ReturnValue
    $Adapter.InsertCommand.Parameters.Add("@BudgetYear",[System.Data.SqlDbType]::Int,-1,"BudgetYear")
    $Adapter.InsertCommand.Parameters.Add("@StartDate",[System.Data.SqlDbType]::Date,-1,"StartYear")
    $Adapter.InsertCommand.Parameters.Add("@EndDate",[System.Data.SqlDbType]::Date,-1,"EndYear")
    $Adapter.InsertCommand.Parameters.Add("@IsActive",[System.Data.SqlDbType]::Bit,-1,"IsActive")
    $Adapter.InsertCommand.Parameters.Add("@ModifiedBy",[System.Data.SqlDbType]::NVarChar,-1,"ModifiedBy")
    $Adapter.InsertCommand.Parameters.Add("@ModifiedOn",[System.Data.SqlDbType]::DateTime,-1,"ModifiedOn")

}

$Adapter.Update($Global:dtCalendars)                       

谢谢

A Print screen of my code

1 个答案:

答案 0 :(得分:0)

通过阅读错误消息,似乎参数@StartDate未在您的代码中传递。您可以传递此信息,但实际值可能为null。因此,在传递参数之前进行空检查,或确保@StartDate具有值。

为了解决这个问题,我首先要对参数@StartDate进行硬编码并尝试。