Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top