Hi guys,
I’m working with DataTable.
I’m bringing my data from SQL Server Table, I need to iterate through some rows in the table based on specific criteria and modify records accordingly.
The table has multiple primary key columns.
I know the syntax for one primary key column but not multiple
For simplicity, I have my syntax below
can anyone please tell me how to write the syntax for multiple primary key column?
Here is my script:
[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
#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
#Set Primary Key on Columns: Account|Property|Date
[System.Data.DataColumn[]] $KeyColumn = New-Object System.Data.DataColumn[3]
$KeyColumn[0] = $dtGL.Columns["Account"]
$KeyColumn[1] = $dtGL.Columns["Property"]
$KeyColumn[2] = $dtGL.Columns["Date"]
$dtGL.PrimaryKey = $KeyColumn
If you run it on your end you should get the same error that I’m getting:
New-Object : Cannot find type [System.Data.DataColumn[3]]: verify that the assembly containing this type is loaded.
At C:\Users\jabou-ghannam\OneDrive - Island Hospitality\Island Hospitality\PowerShell\Begining PowerShell Scripting for Developers\Working With
DataTable.ps1:28 char:41
- … .Data.DataColumn] $KeyColumn = New-Object System.Data.DataColumn[3]
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : InvalidType: (
[New-Object], PSArgumentException - FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
- CategoryInfo : InvalidType: (