C# .NET ManagementObjectSearcher 查询

发布于 2024-09-26 05:30:52 字数 546 浏览 4 评论 0原文

我的 C# .NET 2.0 应用程序使用 ManagementObjectSearcher 类执行两个查询:

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

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

我想将它们组合起来,以便 _searcher 包含两个查询的所有结果。但是,当我尝试这样做时...

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

...抛出“无效查询”异常。有人对我如何使这项工作有任何想法吗?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏の忆 2024-10-03 05:30:52

遗憾的是,WMI 查询语言不支持 joinunion 操作,因此您必须单独运行这些查询(因为从不同的对象中选择)商店)。

WMI 查询语言 (WQL) 是 ANSI SQL 的子集 — 具有一些语义更改。并非所有在 SQL 中能做的事情都可以在 WQL 中实现。您可以查看 WMI 支持的查询构造 在线访问 MSDN。

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.

那些过往 2024-10-03 05:30:52

我能够使用多个表运行查询,如下所示

 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 
 }

希望这有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文