繁体   English   中英

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

[英]powershell: Exception calling “Fill” with “1” argument(s): “Incorrect syntax near '/'.” '

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

错误,0,作业失败。
03/19/2015 12:51:59,一个作业步骤在 PowerShell 脚本的第 75 行收到错误。 对应的行是“$SqlAdapter2.Fill($DataSet2)”。 更正脚本并重新安排作业。 PowerShell 返回的错误信息是:“用“1”个参数调用“填充”的异常:“'/'附近的语法不正确。” '。 进程退出代码 -1。 步骤失败。,00:00:02,0,0,,,,0

我的代码是:

#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

您尚未打开连接。 这就是问题所在。

在这一行之后:

$connection2.ConnectionString = $connectionString2

添加这一行:

$connection2.Open()

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM