0

I have the following code:

$ItemsToItemGroups | Where-Object { $_.ItemGroupId -ne $null } | ForEach-Object {
    $Query = "INSERT INTO dbo.Items (ArticleNumber, ArticleNameDE, ArticleNameFR, ItemGroupId) VALUES ('{0}','{1}','{2}','{3}')" -f $_.ArticleNumber, $_.ArticleNameDE, $_.ArticleNameFR, $_.ItemGroupId
    Invoke-SqlCmd -ConnectionString $NewDBConnectionString -Query $Query
}

My problem is, that the string that gets inserted into the ArticleNameFR column sometimes is a string like this à l'anglaise - that breaks the insert statement, because of the single quote.

Is there an easy way how I can escape it in my statement so it doesn't break the operation?

I insert around 3000 rows and 95% work perfectly, but some throw an error because of the above.

I could simply replace the ' character in the string before I read it into the DB but I'd rather not do that because the data wouldn't be 100% correct then.

Dale K
  • 25,246
  • 15
  • 42
  • 71
SimonS
  • 1,891
  • 1
  • 29
  • 53

0 Answers0