【问题标题】:Exception calling "ExecuteReader" with "0" argument(s): "Incorrect syntax near '='."使用“0”参数调用“ExecuteReader”的异常:“'=' 附近的语法不正确。”
【发布时间】:2018-10-12 17:39:19
【问题描述】:

我正在编写一个带有 SQL 查询的 powershell 代码,以从我们的一个数据库中获取信息,然后将该信息放入一个变量中以供进一步使用。 我的整个代码都能正常工作,并且完全符合我的要求。

但是在这段代码的 sn-p 中的某个地方产生了两个错误,如果脚本实际上正在执行它应该执行的操作,我无法弄清楚为什么会发生。

$Server = "######"
$Database = "######"
$UserSqlQuery = "SELECT [UdstyrsId]
      ,[Model]
      ,[Serienr]
      ,[Udlevdato]
      ,[Repnr]
      ,[Notat]
  FROM [IT-Support].[dbo].[Udstyrsoplysninger] where aflevdato is null and repnr = $repnr"

    function ExecuteSqlQuery($Server, $Database, $UserSQLQuery) 
    {
        $Datatable = New-Object System.Data.DataTable

        $Connection = New-Object System.Data.SQLClient.SQLConnection
        $Connection.ConnectionString = "server='$Server';database='$Database';integrated security=true;"
        $Connection.Open()
        $Command = New-Object System.Data.SQLClient.SQLCommand
        $Command.Connection = $Connection
        $Command.CommandText = $UserSQLQuery
        $Reader = $Command.ExecuteReader();
        $Datatable.Load($Reader);
        $Connection.Close()

        return $Datatable
    }    
        $ResultsDataTable = New-Object System.Data.DataTable
        $ResultsDataTable = ExecuteSqlQuery $Server $Database $UserSqlQuery  

错误代码:

Exception calling "ExecuteReader" with "0" argument(s): "Incorrect syntax        near '='."
At C:\Users\adm-#######\Desktop\UserResignation.ps1:70 char:13
+             $Reader = $Command.ExecuteReader();
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SqlException

Exception calling "Load" with "1" argument(s): "Value cannot be null.
Parameter name: dataReader"
At C:\Users\adm-#######\Desktop\UserResignation.ps1:71 char:13
+             $Datatable.Load($Reader);
+             ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

第 70 和 71 行是脚本中的这些行:

$Reader = $Command.ExecuteReader();
$Datatable.Load($Reader);

【问题讨论】:

  • 你设置$repnr了吗?似乎您的 $UserSqlQuery 有语法错误,因为您的 where 条件缺少值。
  • @KirillPashkov 嗨,是的,$repnr 是在脚本前面通过 $repnr = (Get-ADuser -identity JohnDoe -properties * | select Office).Office 设置的。
  • $Datatable.Load($Reader) 中的$Reader 的值是多少?顺便提一句。您可以使用 powershell 省略末尾的 ;
  • @SimonBøgelund,好像$repnr 为空或为空。
  • 这让我觉得很奇怪,因为在我发布的代码上方仅几行我将 repnr 保存在 $RepNr = (Get-ADUser -Identity $User -Properties * | Select-Object Office).office .所以它应该被填充。

标签: sql-server powershell


【解决方案1】:

您忘记为查询的where 条件声明$repnr,因此您会收到语法错误where aflevdato is null and repnr =

$repnr = 123
$UserSqlQuery = "SELECT [UdstyrsId]
      ,[Model]
      ,[Serienr]
      ,[Udlevdato]
      ,[Repnr]
      ,[Notat]
  FROM [IT-Support].[dbo].[Udstyrsoplysninger] where aflevdato is null and repnr = $repnr"

【讨论】:

  • 好发现基里尔!
  • 原来是这个原因。在我的脚本前面我有另一个变量: $CN = Get-ADUser -Properties accountexpirationdate -Filter { (Enabled -EQ $True -and AccountExpirationDate -GE 0) } | Where-Object { ($_.distinguishedname -notlike 'Eksterne') } | Select-Object -ExpandProperty DistinguishedName 这将返回我们的 AD 中的所有用户以及到期日期。然后它们通过一个 foreach 循环,其中设置了 $repnr 变量。
  • $repnr 基于 Office 属性中的值。第一行捕获了一个我认为我已经豁免的用户,并且没有填充特定用户的 Office 属性,导致 $repnr 在稍后的脚本中的 SQL 查询中为空。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2011-10-25
  • 1970-01-01
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多