本文介绍了如何在 PowerShell 中遍历数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从 DataSet
中输出每一行的值:
I am trying to output values of each rows from a DataSet
:
for ($i=0;$i -le $ds.Tables[1].Rows.Count;$i++)
{
Write-Host 'value is : ' + $i + ' ' + $ds.Tables[1].Rows[$i][0]
}
给出输出...
value is : +0+ +System.Data.DataSet.Tables[1].Rows[0][0]
value is : +1+ +System.Data.DataSet.Tables[1].Rows[1][0]
value is : +2+ +System.Data.DataSet.Tables[1].Rows[2][0]
value is : +3+ +System.Data.DataSet.Tables[1].Rows[3][0]
value is : +4+ +System.Data.DataSet.Tables[1].Rows[4][0]
value is : +5+ +System.Data.DataSet.Tables[1].Rows[5][0]
value is : +6+ +System.Data.DataSet.Tables[1].Rows[6][0]
如何从列中获取实际值?
How do I get the actual value from the column?
推荐答案
PowerShell 字符串评估正在调用 DataSet 上的 ToString().为了评估任何属性(或方法调用),您必须通过将表达式包含在 $()
The PowerShell string evaluation is calling ToString() on the DataSet. In order to evaluate any properties (or method calls), you have to force evaluation by enclosing the expression in $()
for($i=0;$i -lt $ds.Tables[1].Rows.Count;$i++)
{
write-host "value is : $i $($ds.Tables[1].Rows[$i][0])"
}
另外,foreach
允许您遍历集合或数组,而无需计算长度.
Additionally foreach
allows you to iterate through a collection or array without needing to figure out the length.
重写(并为编译而编辑)-
Rewritten (and edited for compile) -
foreach ($Row in $ds.Tables[1].Rows)
{
write-host "value is : $($Row[0])"
}
这篇关于如何在 PowerShell 中遍历数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!