0

In my c# code I have the following

string date = DateTime.Now.ToString("yyyyMMdd"); 
ObjectQuery queryError = new ObjectQuery("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Application' AND (TimeGenerated > '" + date + "000000.000000-000' AND (Type='Error'))");

1) This works but when I try to ORDER BY TimeGenerated DESC I get invalid query message. How can I do this query ordering by TimeGenerated?

ObjectQuery queryError = new ObjectQuery("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Application' AND (TimeGenerated > '" + date + "000000.000000-000' AND (Type='Error')) ORDER BY TimeGenerated DESC");

2) How can I write the same query limiting the results? I tried LIMIT 20 but I got the same invalid query message.

ObjectQuery queryError = new ObjectQuery("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Application' AND (TimeGenerated > '" + date + "000000.000000-000' AND (Type='Error')) LIMIT 20");

Thanks

user3912327
  • 1
  • 1
  • 7
  • WQL, the WMI Query Language, is a **subset** of SQL. Valid keywords are [listed here](http://msdn.microsoft.com/en-us/library/aa394606%28v=vs.85%29.aspx), you can see that ORDER BY is not supported. – Hans Passant Aug 15 '14 at 02:01
  • Thanks Hans. I see it has a WITHIN Clause but I could not figure how to use it. Do you know how I should use it in my query? – user3912327 Aug 15 '14 at 17:30
  • You don't, it is only valid on the kind of queries that generate an event. Win32_NTLogEvent has no events. Events as in "Button.Click event", not "event log". – Hans Passant Aug 15 '14 at 18:11

0 Answers0