在PowerShell中处理System.DBNull [英] Dealing with System.DBNull in PowerShell
问题描述
编辑:从PowerShell 7 Preview 2开始,-不是[System.DBNull] :: Value
的评估结果为 $ true
,感谢 Joel Sallow 通过拉出请求9794
As of PowerShell 7 Preview 2, -not [System.DBNull]::Value
evaluates to $true
, thanks to Joel Sallow via pull request 9794
花更多时间在PowerShell中提取SQL数据。遇到[System.DBNull] :: Value问题以及在比较期间PowerShell的行为方式。
Spending more time pulling SQL data in PowerShell. Running into issues with [System.DBNull]::Value and how PowerShell behaves with this during comparisons.
这是我看到的行为示例,以及解决方法
Here's an example of the behavior I see, along with workarounds
#DBNull values don't evaluate like Null...
if([System.DBNull]::Value){"I would not expect this to display"}
# The text displays.
if([string][System.DBNull]::Value){"This won't display, but is not intuitive"}
# The text does not display.
#DBNull does not let you use certain comparison operators
10 -gt [System.DBNull]::Value
# Could not compare "10" to "". Error: "Cannot convert value "" to type "System.Int32". Error: "Object cannot be cast from DBNull to other types.""
[System.DBNull]::Value -gt 10
# Cannot compare "" because it is not IComparable.
#No real workaround. Must use test for null workaround in conjunction to avoid comparison altogether:
[string][System.DBNull]::Value -and [System.DBNull]::Value -gt 10
#Example scenario with a function that uses Invoke-Sqlcmd2 to pull data
Get-XXXXServer | Where-Object{$_.VCNumCPUs -gt 8}
#Error for every line where VCNumCPU has DBNull value
#workaround
Get-XXXXServer | Where-Object{[string]$_.VCNumCPUs -and $_.VCNumCPUs -gt 8}
我是否遗漏了任何东西,或者没有简单的解决方法可以让缺乏经验的人按预期使用PowerShell比较?
Am I missing anything, or is there no 'simple' workaround for this that would let folks with little experience use PowerShell comparisons as expected?
我提交了对Connect的建议,并有一个临时< Dave Wyatt提供的href = http://powershell.org/wp/forums/topic/dealing-with-dbnull/ rel = nofollow noreferrer>解决方法,该方法将数据行转换为psobject,而dbnull转换为null ,但是会增加一些开销。
I submitted a suggestion on Connect and have a temporary workaround from Dave Wyatt that converts datarows to psobjects with dbnulls converted to nulls, but this adds a bit of overhead. Seems like something that should be handled under the covers, given the existing 'loose' behavior of PowerShell?
任何提示,还是我现在已经精疲力尽了?
Any tips, or have I exhausted my options for now?
p>
谢谢!
Thanks!
推荐答案
我认为您在这里采用了错误的方法。如文档所述, DBNull
类表示一个不存在的值,因此类似 -gt
或 -lt
的比较没有任何意义不存在的值既不大于也不小于任何给定值。 值$ c不过,$ c>
字段具有 Equals()
方法,该方法可让您检查值是否为 DBNull
:
I think you're taking a wrong approach here. As documented, the DBNull
class represents a non-existing value, so comparisons like -gt
or -lt
don't make any sense. A value that doesn't exist is neither greater nor less than any given value. The Value
field has an Equals()
method, though, which allows you to check if a value is or isn't DBNull
:
PS C:> ([DBNull]::Value).Equals(23)
False
PS C:> ([DBNull]::Value).Equals([DBNull]::Value)
True
这篇关于在PowerShell中处理System.DBNull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!