C# Powershell中的DataTable.Rows.Find(MultiplePrimaryKey)
我正在使用一个SQL Server表,我将作为一个datatable进行一些修改,并将数据保存回数据库 我现在一直在为DataTable.Rows.Find(PrimaryKey)寻找正确的语法 为了简单起见,我创建了一个包含一些数据的简单数据表 您也可以在您的终端进行测试 这是我的语法C# Powershell中的DataTable.Rows.Find(MultiplePrimaryKey),c#,.net,vb.net,powershell,datatable,C#,.net,Vb.net,Powershell,Datatable,我正在使用一个SQL Server表,我将作为一个datatable进行一些修改,并将数据保存回数据库 我现在一直在为DataTable.Rows.Find(PrimaryKey)寻找正确的语法 为了简单起见,我创建了一个包含一些数据的简单数据表 您也可以在您的终端进行测试 这是我的语法 [System.Data.DataTable]$dtGL = New-Object System.Data.DataTable("GL") #Schemas $dtGL.Columns.Add("Account
[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
需要一个对象数组。在编辑之前,您显示您正在传入一个字符串数组。如果每个主键列都是字符串类型,那么这将起作用。因为您有一个DateTime,所以需要传递一个对象数组,并且Date列的值应该是DateTime类型
尝试将实现KeyToFind的方式更改为对象数组,同时强制转换每种不同的类型:
[Object[]]$KeyToFind = [String]'00001', [String]'1001', [DateTime]'01/01/2018'
这应该很有效,我尝试了以下方法:
$P01 = '00001'
$P02 = '1001'
$P03 = '01/01/2018'
$FoundRows = $dtGL.Rows.Find(@($P01,$P02,$P03))
如果你给三个参数而不是把它们包装成一个数组,这行吗that@Codernator您应该使用此附加信息和新的确切错误消息(无过载…)编辑您的问题