Testing DataSet and/or DataTable is null

Dave Gray 586 Reputation points
2021-04-19T14:06:00.727+00:00

Hello

Can anyone please tell me how I can test a DataSet contains at least a .Tables[0] or a DataTable is not null?

For in the mocked up example, where 1=2 is always going to return null and therefore both the commands fail.

In production I would be returning null 99% of the time so I need to test for it and move on when null.

89130-ps3.png

$DS = (Invoke-Sqlcmd -ServerInstance "server" -Username "user" -Password "password" -Database "database" -Query "select 1,2,3 where 1=2" -As DataSet)
$DS.GetType()

$DT = (Invoke-Sqlcmd -ServerInstance "server" -Username "user" -Password "password" -Database "database" -Query "select 1,2,3 where 1=2" -As DataSet).Tables[0]
$DT.GetType()

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. Dave Gray 586 Reputation points
    2021-04-19T14:48:22.15+00:00

    The obvious answer is below :-)

    if ($DS -ne $null)
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.