我需要使用toString()对象的方法并将其放入数组中。我想知道,如果我将数组转换为 a String[],它会使用该toString()方法吗?
例如:
public static String[] toStringArray(MyObject[] myObjects) {
// Will this return the toString() representation of each object?
return (String[])myObjects;
}
Run Code Online (Sandbox Code Playgroud) 我一直在处理这个问题.我有一个简单的PowerShell函数,它接受SQL Server的开放连接和包含SQL命令的String,并对服务器运行它.该函数应返回一个整数值,表示已更新的行数.但是,它正在输出有关该方法的信息.
码:
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)
}
Run Code Online (Sandbox Code Playgroud)
但是,执行时,我收到以下错误:
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)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ …Run Code Online (Sandbox Code Playgroud)