开发者

C# .NET ManagementObjectSearcher Query

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 que开发者_开发知识库ries. 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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜