3

My C# .NET 2.0 application performs two queries using the ManagementObjectSearcher class:

_searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSWmi_PnPInstanceNames");

_searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");

I would like to combine them, so that _searcher contains all the results from both queries. However, when I try to do this...

_searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSWmi_PnPInstanceNames AND MSSerial_PortName");

...an "Invalid query" exception is thrown. Does anyone have any ideas as to how I can make this work? Thanks.

RRUZ
  • 134,889
  • 20
  • 356
  • 483
Jim Fell
  • 13,750
  • 36
  • 127
  • 202
  • 1
    That wouldn't be a sane way to write a query in any SQL dialect I can think of. If the `select` statements return the same rowtype, you could try running a `union` of the two selects (don't know if this would work with wmi though). – ChristopheD Oct 04 '10 at 20:15

2 Answers2

4

Unfortunately, the WMI query language does not support join or union operations, so you have to run these queries separately (since the select from different object stores).

The WMI Query Language (WQL) is a subset of ANSI SQL — with some semantic changes. Not everything you can do in SQL is possible in WQL. You can see the WMI supported query constructs online at MSDN.

LBushkin
  • 129,300
  • 32
  • 216
  • 265
1

I am able to run query with multiple tables as shown below

 ManagementObjectSearcher searcher; 
 query = new ObjectQuery(string.Format("Select SMS_CollectToSubCollect.subCollectionID,SMS_Collection.Name " +
                                        " from SMS_CollectToSubCollect, SMS_Collection " +
                                        " where  subCollectionID in " +
                                        " (select CollectionID  from SMS_Collection where name='{0}') " +
                                        " and SMS_Collection.CollectionID  =  SMS_CollectToSubCollect.parentCollectionID " , name));
foreach (ManagementObject queryObj in searcher.Get())
{
//TODO:read values from results 
 }

Hope this helps

Ram
  • 15,908
  • 4
  • 48
  • 41