Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Writing to MSSQL database error Incorrect Syntax near '('

$
0
0

I have the following code below,  I am trying to write the data to a table, but it keeps throwing up the following error.  As you can see, I've tried several ways.  I just can't find the issue..  Thank you for your help


Exception calling "ExecuteNonQuery" with "0" argument(s): "Incorrect syntax near '('."
At C:\Scripts\Sharepoint\LastUserLogon\ParseFile.ps1:111 char:1
+ $Cmd.ExecuteNonQuery();
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SqlException


Foreach ($item in $tbl_2)
{
$OU = $item.OU
$Type = $item.type -replace " ", ""

If ($OU -like "*$OU")
{
Write-Host $sam $OU $itemType $Date $OU
$csv = @"
"$sam",$Type","$LastLogon","$Enabled","$DN","$OU"
"@
$csv >>$ParseOutcsv


#$Cmd.CommandText = "INSERT INTO $tbl_3 (sam,Type,Enabled,Date,DN,OU) VALUES ('$sam','$Type','$lastLogon','$Enabled','$DN','$OU')"
#$cmd.ExecuteNonQuery()



$Cmd.Parameters.Add("@sam", $sam);
$Cmd.Parameters.Add("@Type", $Type);
$Cmd.Parameters.Add("@lastLogon", $lastLogon);
$Cmd.Parameters.Add("@Enabled", $Enabled);
$Cmd.Parameters.Add("@DN", $DN);
$Cmd.Parameters.Add("@OU", $OU);


$Cmd.CommandText = "INSERT INTO $tbl_3 (Sam,Type,Enabled,Date,DN,OU) VALUES (@sam,@Type,@lastLogon,@Enabled,@DN,@OU)";
$Cmd.ExecuteNonQuery();

}


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>