簡體   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