我传入了Oracle插入参数的哈希表。哈希表Value的内容是一个数组。我一直收到这个错误:
Exception setting "Value": "Value does not fall within the expected range."
当我尝试使用数组绑定添加值时。我有一个有效的命令对象,我将ArrayBindCount设置为正确的值。这里是我使用的代码和输出。这应该工作!请注意,$ parameterValues变量包含我传入的哈希表:
$cmdObject = New-Object Oracle.DataAccess.Client.OracleCommand($sqlStatement,$connectionObject)
$cmdObject.BindByName = $true
if($parameterValues)
{
ForEach($p in $parameterValues.GetEnumerator())
{
$cmdObject.ArrayBindCount = $p.Value.Count
Write-Host ("ParameterName = ""{0}"" type = {1} Parameter Value Type = {2} Count = {3}" -f $p.Key, $p.Key.GetType().FullName, $p.Value.GetType().FullName, $p.Value.Count)
$oraParam = New-Object Oracle.DataAccess.Client.OracleParameter
$oraParam.ParameterName = $p.Key
$oraParam.Value = $p.Value
$cmdObject.Parameters.Add($oraParam) | Out-Null
}
Write-Host("Number of parameters in `$cmdObject.Parameters = {0}" -f $cmdObject.Parameters.Count)
Write-Host ("Value of `$cmdObject.ArrayBindCount = {0}" -f $cmdObject.ArrayBindCount)
以下是我得到的输出和错误:
Value of $cmdObject.CommandText = insert into regions (region_id, region_name) values (:REGION_ID, :REGION_NAME)
ParameterName = "REGION_NAME" type = System.String Parameter Value Type = System.String[] Count = 4
Exception setting "Value": "Value does not fall within the expected range."
At C:\Users\areum\Downloads\wd\Scratch\HelloPSWorld_functions.ps1:1615 char:4
+ $oraParam.Value = $p.Value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
ParameterName = "REGION_ID" type = System.String Parameter Value Type = System.Int32[] Count = 4
Exception setting "Value": "Value does not fall within the expected range."
At C:\Users\areum\Downloads\wd\Scratch\HelloPSWorld_functions.ps1:1615 char:4
+ $oraParam.Value = $p.Value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Number of parameters in $cmdObject.Parameters = 2
Value of $cmdObject.ArrayBindCount = 4
我无法让这个工作!请帮忙!
答案 0 :(得分:0)
这个例外来自Oracle。看起来你只能提供一个int,而不是一个字符串。如果您的值是一个可以转换为int的字符串,请使用[int] $ p.Value。否则,它可能是一个旧学校枚举(只是一个数字,而不是一个适当的结构)。如果是这样,请查找该枚举直接转换为int(尝试在线搜索特定字符串,您应该找到有关它的实际值的文档)。
故事的寓意是:
当您在PowerShell中看到异常时,请注意它。它很少来自PowerShell层,通常来自表面下方的.NET。