如何循环使用powerhell中的数据集? [英] How to loop through a dataset in powershell?

查看:140
本文介绍了如何循环使用powerhell中的数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个非常简单的任务,从 DataSet 中输出每行的值:

 for($ i = 0; $ i -le $ ds.Tables [1] .Rows.Count; $ i ++)
{
写入主机' :'+ $ i +''+ $ ds.Tables [1] .Rows [$ i] [0]
}

给出输出...

 值为:+0+ + System.Data.DataSet .Tables [1] .Rows [0] [0] 
值为:+1+ + System.Data.DataSet.Tables [1] .Rows [1] [0]
值为:+ 2+ + System.Data.DataSet.Tables [1] .Rows [2] [0]
值为:+3+ + System.Data.DataSet.Tables [1] .Rows [3] [0]
值为:+4+ + System.Data.DataSet.Tables [1] .Rows [4] [0]
值为:+5+ + System.Data.DataSet.Tables [1] .Rows [5] [0]
的值是:+6+ + System.Data.DataSet.Tables [1] .Rows [6] [0]
pre>

如何从列中获取实际值?

解决方案

p> PowerShell字符串评估是在DataSet上调用ToString()。为了评估任何属性(或方法调用),您必须通过在 $()



中包含表达式来强制评估

  for($ i = 0; $ i -lt $ ds.Tables [1] .Rows.Count; $ i ++)
{
write-host 的价值是:$ i $($ ds.Tables [1] .Rows [$ i] [0])
}

另外, foreach 允许您遍历集合或数组,而不需要确定长度。



重写(编辑) -

  foreach($ Row in $ ds.Tables [1 ] .Rows)
{
write-host的值为:$($ Row [0])
}


I am trying a "very" simple task 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]
}

gives output ...

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?

解决方案

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])"
}

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])"
}

这篇关于如何循环使用powerhell中的数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆