从SQL查询返回到PowerShell变量的内容是什么?

时间:2017-10-16 21:11:48

标签: sql powershell variables

这是我设置的功能,可以很好地从PowerShell向SQL数据库发送查询并返回结果(结果是我不太了解的)

deletePost(postId, pictureStorageUri, thumbStorageUri): Promise<any> {
        console.log(`Deleting ${postId}`);

        // CONSTRUCT/DEFINE update object for real-time database data deletion
        const updateObject = {};
        updateObject[`/posts/${postId}`] = null;
        updateObject[`/people/${this.currentUser.uid}/posts/${postId}`] = null;
        updateObject[`/feed/${this.currentUser.uid}/${postId}`] = null;
        updateObject[`/comments/${postId}`] = null;
        updateObject[`/likes/${postId}`] = null;

        // DEFINE/CALL deleteFromDatabase promise
        let promise = this.db.ref().update(updateObject);

        // IF Picture Uris DEFINE/CALL storage delete promises
        if (pictureStorageUri) {
            const deleteFullFromStorage = this.storage.refFromURL(pictureStorageUri).delete();
            const deleteThumbFromStorage = this.storage.refFromURL.delete();
            console.log('promise being called');
            promise = Promise.all([promise, deleteFullFromStorage, deleteThumbFromStorage]);
        }

        // RETURNS this promise if no picture uris
        return promise;
    }

如果我运行如下所示的查询(它没有返回任何结果,意味着没有与条件匹配的记录),为什么在我放入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 } 时没有返回任何内容?当我$results时,为什么结果为'表'?见下文

Write-Host $results

当没有找到记录时,我认为它等于“”或$ null但不是在测试时

$ null test

PS>$results = Invoke-SQL -server 'servername' -database 'DBname' -Query "SELECT * FROM [DBname].[dbo].[TableName] WHERE UserID = 'x' AND ComputerName = 'x'"
PS>$results
PS>Write-Host $results
Table

“”测试

PS>If ($results -eq $null) {
>> write-host "Null"}else{
>> write-host "Not Null"
>> }
Not Null

如果有人可以向我解释这一点,以及我可能有哪些选项来检查查询是否没有结果,那就太棒了!

1 个答案:

答案 0 :(得分:0)

阅读问题帖子的评论以获取更多详细信息。

为了查看是否返回了记录,这将返回返回的行数(记录)。归功于@Bill_Stewart。

($results | Measure-Object).Count

@Tomalak提供了有用的link

@BaconBits有一个有用的提示来获取对象的类型

$results.GetType().FullName
# or
$results | Get-Member

谢谢大家的帮助。