private static MonitorData setMonitorData() { MonitorData md = new MonitorData(); try { //this queries the information of all available monitors ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT * FROM WmiMonitorID"); foreach (ManagementObject obj in searcher.Get()) { foreach (PropertyData p in obj.Properties) { if (p.Value != null) { switch (p.Value.GetType().ToString()) { case "System.UInt16[]": { //d.description = "Monitor"; switch (p.Name) { case "ManufacturerName": { md.vendorM = getString((UInt16[])p.Value); break; } case "SerialNumberID": { md.serialNumberM = getString((UInt16[])p.Value); break; } case "UserFriendlyName": { md.modelM = getString((UInt16[])p.Value); break; } } break; } } } } } Database d = new Database(); if (!d.verifyMonitor(md.serialNumberM)) { d.insertMonitor(md); } } catch (Exception e) { Console.WriteLine(e.Message); } return(md); }
public void insertMonitor(MonitorData data) { try { sqlConnection.Open(); SqlCommand sqlCommand = new SqlCommand("INSERT INTO Monitor(monitorModel, monitorSerial, monitorVendor) " + "VALUES('" + data.modelM + "','" + data.serialNumberM + "','" + data.vendorM + "')" , sqlConnection); sqlCommand.ExecuteNonQuery(); } catch (SqlException e) { SqlCommand sqlCommand = new SqlCommand("INSERT INTO Monitor(monitorModel, monitorSerial, monitorVendor) " + "VALUES('" + "Generic Model" + "','" + "Generic Serial" + "','" + "Generic Vendor" + "')" , sqlConnection); sqlCommand.ExecuteNonQuery(); Console.WriteLine("Sql error from insertMonitor method" + e.Message); } finally { sqlConnection.Close(); } }