[英]powershell: Exception calling “Fill” with “1” argument(s): “Incorrect syntax near '/'.” '
Can someone please help me with this error:有人可以帮我解决这个错误吗:
Error,0,The job failed.
错误,0,作业失败。
03/19/2015 12:51:59,A job step received an error at line 75 in a PowerShell script.03/19/2015 12:51:59,一个作业步骤在 PowerShell 脚本的第 75 行收到错误。 The corresponding line is ' $SqlAdapter2.Fill($DataSet2)'.
对应的行是“$SqlAdapter2.Fill($DataSet2)”。 Correct the script and reschedule the job.
更正脚本并重新安排作业。 The error information returned by PowerShell is: 'Exception calling "Fill" with "1" argument(s): "Incorrect syntax near '/'."
PowerShell 返回的错误信息是:“用“1”个参数调用“填充”的异常:“'/'附近的语法不正确。” '.
'。 Process Exit Code -1.
进程退出代码 -1。 The step failed.,00:00:02,0,0,,,,0
步骤失败。,00:00:02,0,0,,,,0
My code is:我的代码是:
#Exceute the procedure and place the .csv file on server
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToShortDateString()
$PreviousDate = (Get-Date).AddDays(-1).ToShortDateString()
$server2 = "XXXXXXX"
$database2 = "XXXXXX"
$query2 = "exec XXX.XXXXX_XXXXX_daily_report"+$PreviousDate+","+$PreviousDate
$extractFile2 = "C:\XXX\XXX\XXX\XXX\XXX_daily_Report"+($CurrentDate)+".csv"
$connectionTemplate2 = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString2 = [string]::Format($connectionTemplate2, $server2, $database2)
$connection2 = New-Object System.Data.SqlClient.SqlConnection
$connection2.ConnectionString = $connectionString2
$command2 = New-Object System.Data.SqlClient.SqlCommand
$command2.CommandText = $query2
$command2.Connection = $connection2
$command2.CommandTimeout=0
$SqlAdapter2 = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter2.SelectCommand = $command2
$DataSet2 = New-Object System.Data.DataSet
$SqlAdapter2.Fill($DataSet2)
$connection2.Close()
# dump the data to a csv
$DataSet2.Tables[0] | Export-Csv -NoTypeInformation $extractFile2
You haven't opened the connection yet.您尚未打开连接。 That is the issue.
这就是问题所在。
Right after this line:在这一行之后:
$connection2.ConnectionString = $connectionString2
Add this line:添加这一行:
$connection2.Open()
In the exec statement, you might need to put a space between these two pieces: _daily_report"+$PreviousDate在 exec 语句中,您可能需要在这两部分之间放置一个空格:_daily_report"+$PreviousDate
Also, you can just do this -- write the whole SQL command as one string:此外,您可以这样做——将整个 SQL 命令写为一个字符串:
$query2 = "exec XXX.XXXXX_XXXXX_daily_report $PreviousDate, $PreviousDate" $query2 = "exec XXX.XXXXX_XXXXX_daily_report $PreviousDate, $PreviousDate"
because Powershell will insert the values of those variables into the string for you.因为 Powershell 会为您将这些变量的值插入到字符串中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.