【问题标题】:powershell: Exception calling "Fill" with "1" argument(s): "Incorrect syntax near '/'." 'powershell:使用“1”参数调用“填充”的异常:“'/' 附近的语法不正确。” '
【发布时间】:2015-03-19 20:06:50
【问题描述】:

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

错误,0,作业失败。
2015 年 3 月 19 日 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

【问题讨论】:

  • 我们是否缺少其他代码?看起来您正在创建一个 $DataSet2 对象,然后尝试将其用作 $SqlAdapter2.Fill() 方法的重载。对于这样的方法,您通常需要一些数据作为输入。我觉得很难帮助你,因为我不明白你想在这里做什么。
  • 我很确定问题出在日期的格式上。我不是 SQL 专家,但我会建议像 $PreviousDate = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd") 这样的东西,看看是否能解决您的问题。我不认为 SQL 喜欢日期格式中的斜杠。

标签: powershell


【解决方案1】:

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

就在这一行之后:

$connection2.ConnectionString = $connectionString2

添加这一行:

$connection2.Open()

【讨论】:

    【解决方案2】:

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

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

    $query2 = "执行 XXX.XXXXX_XXXXX_daily_report $PreviousDate, $PreviousDate"

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多