【发布时间】:2018-11-16 12:32:22
【问题描述】:
我正在使用 SQL Server 表,我将作为数据表进行一些修改并将数据保存回数据库。
我现在一直在寻找 DataTable.Rows.Find(PrimaryKey) 的正确语法
为简单起见,我创建了一个包含一些数据的简单数据表 你也可以在你的最后测试它。
这是我的语法。
[System.Data.DataTable]$dtGL = New-Object System.Data.DataTable("GL")
#Schemas
$dtGL.Columns.Add("Account", "String") | Out-Null
$dtGL.Columns.Add("Property", "String") | Out-Null
$dtGL.Columns.Add("Date", "DateTime") | Out-Null
$dtGL.Columns.Add("Amount", "Decimal") | Out-Null
[System.Data.DataColumn[]]$KeyColumn = ($dtGL.Columns["Account"],$dtGL.Columns["Property"],$dtGL.Columns["Date"])
$dtGL.PrimaryKey = $KeyColumn
#Records
$dtGL.Rows.Add('00001','1000','1/1/2018','185') | Out-Null
$dtGL.Rows.Add('00001','1000','1/2/2018','486') | Out-Null
$dtGL.Rows.Add('00001','1001','1/1/2018','694') | Out-Null
$dtGL.Rows.Add('00001', '1001', '1/2/2018', '259') | Out-Null
[String[]]$KeyToFind = '00001', '1001', '01/01/2018'
$FoundRows = $dtGL.Rows.Find($KeyToFind)
$FoundRows | Out-GridView
我收到以下错误
Exception calling "Find" with "1" argument(s): "Expecting 3 value(s) for the key being indexed, but received 1 value(s)."
At C:\Users\MyUserName\OneDrive - MyUserName\MyCompany\PowerShell\Samples\Working With DataTable.ps1:32 char:5
+ $FoundRows = $dtGL.Rows.Find($KeyToFind)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
我也试过把参数分开
$P01 = '00001'
$P02 = '1001'
$P03 = '01/01/2018'
$FoundRows = $dtGL.Rows.Find($P01,$P02,$P03)
这是错误
Cannot find an overload for "Find" and the argument count: "3".
+ $FoundRows = $dtGL.Rows.Find($P01,$P02,$P03)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
【问题讨论】:
-
如果你给出三个参数而不是将它们包装在一个数组中,它会起作用吗?
-
不,我刚试过
-
@Codernator 您应该使用此附加信息和新的确切错误消息(无过载...)编辑您的问题
标签: c# .net vb.net powershell datatable