I am trying a "very" simple task to output values of each rows from a DataSet
:
我正在尝试从“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?
如何从列中获取实际值?
3 个解决方案
#1
30
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 $()
PowerShell字符串评估在DataSet上调用ToString()。为了评估任何属性(或方法调用),您必须通过将表达式括在$()中来强制求值
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.
另外foreach允许您遍历集合或数组,而无需计算长度。
Rewritten (and edited for compile) -
重写(并编辑编译) -
foreach ($Row in $ds.Tables[1].Rows)
{
write-host "value is : $($Row[0])"
}
#2
16
Here's a practical example (build a dataset from your current location):
这是一个实际示例(从您当前位置构建数据集):
$ds = new-object System.Data.DataSet
$ds.Tables.Add("tblTest")
[void]$ds.Tables["tblTest"].Columns.Add("Name",[string])
[void]$ds.Tables["tblTest"].Columns.Add("Path",[string])
dir | foreach {
$dr = $ds.Tables["tblTest"].NewRow()
$dr["Name"] = $_.name
$dr["Path"] = $_.fullname
$ds.Tables["tblTest"].Rows.Add($dr)
}
$ds.Tables["tblTest"]
$ds.Tables["tblTest"]
is an object that you can manipulate just like any other Powershell object:
$ ds.Tables [“tblTest”]是一个可以像任何其他Powershell对象一样操作的对象:
$ds.Tables["tblTest"] | foreach {
write-host 'Name value is : $_.name
write-host 'Path value is : $_.path
}
#3
2
The parser is having trouble concatenating your string. Try this:
解析器在连接字符串时遇到问题。尝试这个:
write-host 'value is : '$i' '$($ds.Tables[1].Rows[$i][0])
Edit: Using double quotes might also be clearer since you can include the expressions within the quoted string:
编辑:使用双引号可能也更清楚,因为您可以在引用的字符串中包含表达式:
write-host "value is : $i $($ds.Tables[1].Rows[$i][0])"
#1
30
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 $()
PowerShell字符串评估在DataSet上调用ToString()。为了评估任何属性(或方法调用),您必须通过将表达式括在$()中来强制求值
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.
另外foreach允许您遍历集合或数组,而无需计算长度。
Rewritten (and edited for compile) -
重写(并编辑编译) -
foreach ($Row in $ds.Tables[1].Rows)
{
write-host "value is : $($Row[0])"
}
#2
16
Here's a practical example (build a dataset from your current location):
这是一个实际示例(从您当前位置构建数据集):
$ds = new-object System.Data.DataSet
$ds.Tables.Add("tblTest")
[void]$ds.Tables["tblTest"].Columns.Add("Name",[string])
[void]$ds.Tables["tblTest"].Columns.Add("Path",[string])
dir | foreach {
$dr = $ds.Tables["tblTest"].NewRow()
$dr["Name"] = $_.name
$dr["Path"] = $_.fullname
$ds.Tables["tblTest"].Rows.Add($dr)
}
$ds.Tables["tblTest"]
$ds.Tables["tblTest"]
is an object that you can manipulate just like any other Powershell object:
$ ds.Tables [“tblTest”]是一个可以像任何其他Powershell对象一样操作的对象:
$ds.Tables["tblTest"] | foreach {
write-host 'Name value is : $_.name
write-host 'Path value is : $_.path
}
#3
2
The parser is having trouble concatenating your string. Try this:
解析器在连接字符串时遇到问题。尝试这个:
write-host 'value is : '$i' '$($ds.Tables[1].Rows[$i][0])
Edit: Using double quotes might also be clearer since you can include the expressions within the quoted string:
编辑:使用双引号可能也更清楚,因为您可以在引用的字符串中包含表达式:
write-host "value is : $i $($ds.Tables[1].Rows[$i][0])"