如何获取DataTable中的值


PowerShell交流中心分类: Powershell基础如何获取DataTable中的值
1
RozenAnnie asked 9 年 ago

我用New-Object System.Data.DataTable创建了一个$dt,然后调用Oracle.DataAccess将表的值填充到$dt中。
一下是输入$dt后的内容。
TIMESTAMP  : 2015/3/25 23:25:37
CARNUM     : 809
DATANUM    : 25994
NEWCARNUM  : 0
NEWDATANUM : 0
现在我想将表中某一项的值赋给其他变量,如NEWDATANUM的值,请问需要如何操作。
我之前设想的是使用$dt.NEWDATANUM,但是得到的是空值。
PS :\> $dt.gettype()

IsPublic IsSerial Name                                     BaseType
——– ——– —-                                     ——–
True     True     DataTable                                System.ComponentModel.MarshalByValueComponent
PS :\> $dt | get-member

TypeName: System.Data.DataRow
Name              MemberType            Definition
—-              ———-            ———-
AcceptChanges     Method                System.Void AcceptChanges()
BeginEdit         Method                System.Void BeginEdit()
CancelEdit        Method                System.Void CancelEdit()
ClearErrors       Method                System.Void ClearErrors()
Delete            Method                System.Void Delete()
EndEdit           Method                System.Void EndEdit()
Equals            Method                bool Equals(System.Object obj)
GetChildRows      Method                System.Data.DataRow[] GetChildRows(string relationName), System.Data.DataRow…
GetColumnError    Method                string GetColumnError(int columnIndex), string GetColumnError(string columnN…
GetColumnsInError Method                System.Data.DataColumn[] GetColumnsInError()
GetHashCode       Method                int GetHashCode()
GetParentRow      Method                System.Data.DataRow GetParentRow(string relationName), System.Data.DataRow G…
GetParentRows     Method                System.Data.DataRow[] GetParentRows(string relationName), System.Data.DataRo…
GetType           Method                type GetType()
HasVersion        Method                bool HasVersion(System.Data.DataRowVersion version)
IsNull            Method                bool IsNull(int columnIndex), bool IsNull(string columnName), bool IsNull(Sy…
RejectChanges     Method                System.Void RejectChanges()
SetAdded          Method                System.Void SetAdded()
SetColumnError    Method                System.Void SetColumnError(int columnIndex, string error), System.Void SetCo…
SetModified       Method                System.Void SetModified()
SetParentRow      Method                System.Void SetParentRow(System.Data.DataRow parentRow), System.Void SetPare…
ToString          Method                string ToString()
Item              ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Item(string co…
CARNUM            Property              System.Decimal CARNUM {get;set;}
DATANUM           Property              System.Decimal DATANUM {get;set;}
NEWCARNUM         Property              System.Decimal NEWCARNUM {get;set;}
NEWDATANUM        Property              System.Decimal NEWDATANUM {get;set;}
TIMESTAMP         Property              System.DateTime TIMESTAMP {get;set;}

1 Answers
1
Best Answer
Mooser Lee 管理员 answered 9 年 ago
for($i=0;$i -le $dt.Rows.Count;$i++)
{ 
write-host "value is : $i $($dt.Rows[$i][0])"
}

$dt.Rows[1][0]
RozenAnnie replied 9 年 ago

谢谢,这下我清楚了。访问第一列应该用$dt.rows[0][0],之前我一直用的$dt.rows[0],所以返回了$dt的全部内容。