这是我用来向SQL发送查询的函数
function Invoke-SQL
{
param (
[string]$server,
[string]$database,
[string]$Query
)
$connectionString = "Data Source=$server; " +
"Integrated Security=SSPI; " +
"Initial Catalog=$database"
$connection = new-object
system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($Query, $connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
$dataSet.Tables
}
当我运行这样的查询时:
Invoke-SQL -server 'ServerName' -database 'DBname' -Query "SELECT * FROM [DBname].[dbo].[TableName] WHERE UserID = '$userid' AND ComputerName = '$computername'"
我收到以下错误:
ERROR: Exception calling "Fill" with "1" argument(s): "Conversion failed when converting from a character string to uniqueidentifier."
PSDesk_Client.ps1 (358, 2): ERROR: At Line: 358 char: 2
ERROR: + $adapter.Fill($dataSet) | Out-Null
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [],
MethodInvocationException
ERROR: + FullyQualifiedErrorId : SqlException
ERROR:
ERROR: Exception calling "Fill" with "1" argument(s): "Conversion failed when converting from a character string to uniqueidentifier."
PSDesk_Client.ps1 (358, 2): ERROR: At Line: 358 char: 2
ERROR: + $adapter.Fill($dataSet) | Out-Null
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [],
MethodInvocationException
ERROR: + FullyQualifiedErrorId : SqlException
ERROR:
似乎有两次这个错误,因为:
我是否让它返回数据或只是发送命令,它对$adapter.Fill($dataSet) | Out-Null
不满意。为什么我的两个uniqueidentifiers导致此错误,我该怎么做才能解决此问题?
尝试手动输入记录时,会抛出错误:
错误消息:Guid应包含32个数字,包含4个破折号。
uniqueidentifier
是否是错误的数据类型?它必须是一个字符串,但不应该要求采用该格式或任何格式