[英]Exception calling “ExecuteReader” with “0” argument(s): “Incorrect syntax near '='.”
I am writing a powershell code with an SQL query to get information from one of our Databases and then put that information into a variable for further use. 我正在使用SQL查询编写Powershell代码,以从我们的一个数据库中获取信息,然后将该信息放入变量中以备将来使用。 My entire code works, and does exactly what i want it to do. 我的整个代码都能正常工作,并且确实可以完成我想要的工作。
But somewhere in this snippet of the code two errors are produced that I can't figure out why happen if the script is actually doing what it's supposed to do. 但是在这段代码片段中的某些地方会产生两个错误,我无法弄清楚如果脚本实际上在执行应该执行的操作,为什么会发生。
$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
Line 70 and 71 is these lines in the script: 第70和71行是脚本中的这些行:
$Reader = $Command.ExecuteReader();
$Datatable.Load($Reader);
You forgot to declare $repnr
for query's where
condition and because of that you get syntax error where aflevdato is null and repnr =
. 您忘记为查询的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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.