Oracle11g 如何解决错误:异常设置";“值”:&引用;值不在预期范围内。”;在powershell脚本中
我正在为Oracle插入传递一个参数哈希表。哈希表值的内容是一个数组。我不断地发现这个错误:Oracle11g 如何解决错误:异常设置";“值”:&引用;值不在预期范围内。”;在powershell脚本中,oracle11g,powershell-3.0,Oracle11g,Powershell 3.0,我正在为Oracle插入传递一个参数哈希表。哈希表值的内容是一个数组。我不断地发现这个错误: Exception setting "Value": "Value does not fall within the expected range." 当我尝试使用数组绑定添加值时。我有一个有效的命令对象,并将ArrayBindCount设置为正确的值。这里是我正在使用的代码和输出。这应该管用!请注意,$parameterValues变量包含我传入的哈希表: $cmdObject = New-Obje
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
我就是做不到!请帮忙 这个例外来自Oracle。看起来您只能提供int,而不能提供字符串。如果您的值是可以转换为int的字符串,请使用[int]$p.value。否则,它可能是一个老式的枚举(只是一个数字,不是一个正确的结构)。如果是这样,请查找该枚举到int的直接转换(尝试在线搜索特定字符串,您应该可以找到有关其实际值的文档) 这个故事的寓意是:
当您在PowerShell中看到异常时,请注意它。它很少来自PowerShell层,通常来自表面之下的.NET。谢谢您的建议。我会试试这个建议,看看是否有效。非常感谢你抽出时间回答我的问题。一旦我测试了你的建议,我会发布更多反馈。