Testing DataSet and/or DataTable is null

Dave Gray 471 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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,630 questions
No comments
{count} votes

Accepted answer
  1. Dave Gray 471 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