SqlClient.ExecuteNonQuery 未返回行数
SqlClient.ExecuteNonQuery not returning number of rows
我已经处理这个问题一段时间了。我有一个简单的 PowerShell 函数,它需要一个与 SQL 服务器的开放连接和一个包含 SQL 命令的字符串,并针对服务器运行它。该函数应该 return 一个整数值,表示已更新的行数。但是,它正在输出有关该方法的信息。
代码:
function Invoke-MSSQLNonQuery () {
param (
[System.Data.SqlClient.SqlConnection]$Connection,
[string]$Command
)
# Create the SQL Command Ojbect
$SQLCommand = New-Object System.Data.SqlClient.SqlCommand
# Set the Command Text and Connection for the Command
$SQLCommand.CommandText=$Command
$SQLCommand.Connection=$Connection
# Run command and return the rows affected
[int]($SQLCommand.ExecuteNonQuery | Out-String)
}
但是,在执行时,出现以下错误:
Cannot convert value "
OverloadDefinitions
-------------------
int ExecuteNonQuery()
int IDbCommand.ExecuteNonQuery()
" to type "System.Int32". Error: "Input string was not in a correct format."
At C:\_Local\playground\Mason\UpdateFromInventory.ps1:153 char:5
+ [int]($SQLCommand.ExecuteNonQuery | Out-String)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
我知道 ExecuteNonQuery
,当通过管道传输到 Out-String
时,正在输出 table 及其重载定义:
OverloadDefinitions
-------------------
int ExecuteNonQuery()
int IDbCommand.ExecuteNonQuery()
我也尝试过不将其通过管道传输到任何东西,return在没有 table header.
的情况下得到了类似的结果
编辑:另外,我应该提一下,我有 95% 的把握 SQL 命令执行成功。我确信这个函数以前工作过一次,甚至 return 输出正确。然而,已经很久没有这样了。
我的问题是:
- 为什么?
- 如何让它输出受影响的行数?
您正在尝试调用不带括号的方法。执行此操作时,语句将显示重载列表(例如 ),而不是实际调用该方法,即使您先将其转换为字符串,也无法将其转换为整数。
改变
[int]($SQLCommand.ExecuteNonQuery | Out-String)
进入
[int]$SQLCommand.ExecuteNonQuery()
代码应该可以满足您的需求。
我已经处理这个问题一段时间了。我有一个简单的 PowerShell 函数,它需要一个与 SQL 服务器的开放连接和一个包含 SQL 命令的字符串,并针对服务器运行它。该函数应该 return 一个整数值,表示已更新的行数。但是,它正在输出有关该方法的信息。
代码:
function Invoke-MSSQLNonQuery () {
param (
[System.Data.SqlClient.SqlConnection]$Connection,
[string]$Command
)
# Create the SQL Command Ojbect
$SQLCommand = New-Object System.Data.SqlClient.SqlCommand
# Set the Command Text and Connection for the Command
$SQLCommand.CommandText=$Command
$SQLCommand.Connection=$Connection
# Run command and return the rows affected
[int]($SQLCommand.ExecuteNonQuery | Out-String)
}
但是,在执行时,出现以下错误:
Cannot convert value "
OverloadDefinitions
-------------------
int ExecuteNonQuery()
int IDbCommand.ExecuteNonQuery()
" to type "System.Int32". Error: "Input string was not in a correct format."
At C:\_Local\playground\Mason\UpdateFromInventory.ps1:153 char:5
+ [int]($SQLCommand.ExecuteNonQuery | Out-String)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
我知道 ExecuteNonQuery
,当通过管道传输到 Out-String
时,正在输出 table 及其重载定义:
OverloadDefinitions
-------------------
int ExecuteNonQuery()
int IDbCommand.ExecuteNonQuery()
我也尝试过不将其通过管道传输到任何东西,return在没有 table header.
的情况下得到了类似的结果编辑:另外,我应该提一下,我有 95% 的把握 SQL 命令执行成功。我确信这个函数以前工作过一次,甚至 return 输出正确。然而,已经很久没有这样了。
我的问题是:
- 为什么?
- 如何让它输出受影响的行数?
您正在尝试调用不带括号的方法。执行此操作时,语句将显示重载列表(例如
改变
[int]($SQLCommand.ExecuteNonQuery | Out-String)
进入
[int]$SQLCommand.ExecuteNonQuery()
代码应该可以满足您的需求。