0

I am importing a .csv into a variable $user. I can then access each field using $user.ARN (for example). This works fine.

But then I need to build a string using this variable.

$SQLQuery = "USE Cardpresso INSERT INTO dbo.students (Name, ARN, CardNumber, isPrinted) VALUES('$DisplayName', $User.ARN , $CardNumber, 0);"

When I check what is inside the $SQLQuery variable, it holds the whole csv row inplace of $User.ARN.

USE Cardpresso INSERT INTO dbo.students (Name, ARN, CardNumber, isPrinted) VALUES('%%% %%%', @{FirstName=%%%; LastName=%%%; ARN=%%%; Group=%%%; Email=unknown@unknow.com; Pass=%%%; Site=%%%; CardNumber=}.ARN , 508, 0);

(The %%% are real data, just removed)

Why is the $User.ARN not just been replaced with the data from just that field, instead it is inputting the whole row?

Thanks

markdem
  • 51
  • 1
  • 8

1 Answers1

0

For a file "sample.csv" with contents:

name,age,gender
billy,22,M
suzie,26,F
mikey,19,M
Tez,22,F

I'm importing the CSV, and I'm crafting insert statements from each record:

$stuff = Import-Csv -Delimiter ',' -Path .\sample.csv

foreach ($thing in $stuff) {
echo "INSERT INTO Cardpresso.dbo.students (Name, Age, Gender) VALUES ('$(${thing}.name), $(${thing}.age), '$(${thing}.gender)');" 
}

Live example...

enter image description here

Good luck.

Adam
  • 3,891
  • 3
  • 19
  • 42