powershell:使用“1”参数调用“Fill”的异常:“'/'附近的语法不正确。” “

时间:2015-03-19 20:06:50

标签: powershell

有人可以帮我解决这个错误。

错误,0,作业失败。
2015年3月19日12:51:59,作业步骤在PowerShell脚本的第75行收到错误。相应的行是' $ SqlAdapter2.Fill($ DataSet2)&#39 ;.更正脚本并重新安排作业。 PowerShell返回的错误信息是:'异常调用"填写"用" 1"参数:"' /'附近的语法不正确。" &#39 ;.处理退出代码-1。步骤失败。,00:00:02,0,0 ,,,, 0

我的代码是:

例外程序并将.csv文件放在服务器上

$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

2 个答案:

答案 0 :(得分:0)

您还没有打开连接。这就是问题所在。

在这一行之后: $ connection2.ConnectionString = $ connectionString2

添加此行: $ connection2.Open()

答案 1 :(得分:-1)

在exec语句中,您可能需要在这两个部分之间放置一个空格: _daily_report&#34 + $ PreviousDate

此外,您可以这样做 - 将整个SQL命令编写为一个字符串:

$ query2 =" exec XXX.XXXXX_XXXXX_daily_report $ PreviousDate,$ PreviousDate"

因为Powershell会将这些变量的值插入到字符串中。

相关问题