public Pc GetNetworkSystemInfo2() { Pc pcinfo = new Pc(); //Create an object of ManagementObjectSearcher class and pass query as parameter. ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration WHERE DHCPEnabled = 'TRUE'"); pcinfo.Name = GetComputerName(); foreach (ManagementObject managementObject in mos.Get()) { if (managementObject["MACAddress"] != null) { pcinfo.Adress_Mac = managementObject["MACAddress"].ToString(); } if (managementObject["IPAddress"] != null) { string[] val = (string[])managementObject["IPAddress"]; //.Cast<string>().ToArray(); pcinfo.IP = val[0].ToString(); } } return(pcinfo); }
public void SendData() { //Performance Perf = SystemInfo.GetPerformance(); //Instance SystemInfo system = new SystemInfo(); Performance perf = system.GetPerformance(); InfoSystem infosys = system.GetOperatingSystemInfo(); InfoNetwork infoNet = system.GetNetworkSystemInfo(); Pc pcinfo = system.GetNetworkSystemInfo2(); //string CONNECTION_STRING = GetConnexionStringFromFile(); /******************Table InfoNetwork*****************************/ using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) { try { //string Query2 = "Insert into InfoSystems (ComputerName,OS_Architecture,OsSystem,OS_Version,CPU_Physical_Core,CPU_Logical_Core) VALUES (@ComputerName,@OS_Architecture,@OsSystem,@OS_Version,@CPU_Physical_Core,@CPU_Logical_Core)"; string Query3 = "IF EXISTS (SELECT * FROM InfoNetworks WHERE ComputerName like @ComputerName) BEGIN Update InfoNetworks SET " + "IP_Adress = @IP_Adress, MAC_Adress = @MAC_Adress WHERE ComputerName like @ComputerName END " + "ELSE BEGIN Insert into InfoNetworks (ComputerName,IP_Adress,MAC_Adress)" + " VALUES (@ComputerName,@IP_Adress,@MAC_Adress) END"; using (SqlCommand command = new SqlCommand(Query3, conn)) { conn.Open(); command.Parameters.AddWithValue("@ComputerName", infoNet.ComputerName); command.Parameters.AddWithValue("@IP_Adress", infoNet.IP_Adress); command.Parameters.AddWithValue("@MAC_Adress", infoNet.MAC_Adress); command.ExecuteNonQuery(); } } catch (SqlException ex) { } finally { if (conn.State == System.Data.ConnectionState.Open) { conn.Close(); } } } /************************table pc*********************************/ using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) { try { //string Query2 = "Insert into InfoSystems (ComputerName,OS_Architecture,OsSystem,OS_Version,CPU_Physical_Core,CPU_Logical_Core) VALUES (@ComputerName,@OS_Architecture,@OsSystem,@OS_Version,@CPU_Physical_Core,@CPU_Logical_Core)"; string Query4 = "IF EXISTS (SELECT * FROM Pcs WHERE Name like @Name) BEGIN Update Pcs SET " + "IP = @IP, Adress_Mac = @Adress_Mac WHERE Name like @Name END " + "ELSE BEGIN Insert into Pcs (IP,Adress_Mac,Name)" + " VALUES (@IP,@Adress_Mac,@Name) END"; using (SqlCommand command = new SqlCommand(Query4, conn)) { conn.Open(); command.Parameters.AddWithValue("@IP", pcinfo.IP); command.Parameters.AddWithValue("@Adress_Mac", pcinfo.Adress_Mac); command.Parameters.AddWithValue("@Name", pcinfo.Name); command.ExecuteNonQuery(); } } catch (SqlException ex) { } finally { if (conn.State == System.Data.ConnectionState.Open) { conn.Close(); } } } /***************Tables Performances****************************/ using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) { try{ //string Query1 = "Insert into Performances (ComputerName,CPU,RAM,System_Up_Time) VALUES (@ComputerName,@CPU,@RAM,@System_Up_Time)"; string Query1 = "IF EXISTS (SELECT * FROM Performances WHERE ComputerName like @ComputerName) BEGIN Update Performances SET " + "CPU = @CPU, RAM = @RAM, System_Up_Time=@System_Up_Time, ProcName=@ProcName ,DateCreation = GetDate() WHERE ComputerName like @ComputerName END " + "ELSE BEGIN Insert into Performances (ComputerName,CPU,RAM,System_Up_Time, ProcName,DateCreation)" + " VALUES (@ComputerName,@CPU,@RAM,@System_Up_Time, @ProcName , GetDate()) END"; using (SqlCommand command = new SqlCommand(Query1, conn)) { conn.Open(); command.Parameters.AddWithValue("@ComputerName", perf.ComputerName); command.Parameters.AddWithValue("@CPU", perf.CPU); command.Parameters.AddWithValue("@RAM", perf.RAM); command.Parameters.AddWithValue("@System_Up_Time", perf.System_Up_Time); command.Parameters.AddWithValue("@ProcName", perf.ProcName); //command.Parameters.AddWithValue("@DateCreation", perf.DateCreation.Date); command.ExecuteNonQuery(); } } catch (SqlException ex) { } finally { if (conn.State == System.Data.ConnectionState.Open) { conn.Close(); } } } //string CONNECTION_STRING = GetConnexionStringFromFile(); /******************Table InfoSystems*****************************/ using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) { try { //string Query2 = "Insert into InfoSystems (ComputerName,OS_Architecture,OsSystem,OS_Version,CPU_Physical_Core,CPU_Logical_Core) VALUES (@ComputerName,@OS_Architecture,@OsSystem,@OS_Version,@CPU_Physical_Core,@CPU_Logical_Core)"; string Query2 = "IF EXISTS (SELECT * FROM InfoSystems WHERE ComputerName like @ComputerName) BEGIN Update InfoSystems SET " + "OS_Architecture = @OS_Architecture, OsSystem = @OsSystem, OS_Version = @OS_Version,CPU_Physical_Core = @CPU_Physical_Core,CPU_Logical_Core = @CPU_Logical_Core WHERE ComputerName like @ComputerName END " + "ELSE BEGIN Insert into InfoSystems (ComputerName,OS_Architecture,OsSystem,OS_Version,CPU_Physical_Core,CPU_Logical_Core)" + " VALUES (@ComputerName,@OS_Architecture,@OsSystem,@OS_Version,@CPU_Physical_Core,@CPU_Logical_Core) END"; using (SqlCommand command = new SqlCommand(Query2, conn)) { conn.Open(); command.Parameters.AddWithValue("@ComputerName", infosys.ComputerName); command.Parameters.AddWithValue("@OS_Architecture", infosys.OS_Architecture); command.Parameters.AddWithValue("@OsSystem", infosys.OsSystem); command.Parameters.AddWithValue("@OS_Version", infosys.OS_Version); command.Parameters.AddWithValue("@CPU_Physical_Core", infosys.CPU_Physical_Core); command.Parameters.AddWithValue("@CPU_Logical_Core", infosys.CPU_Logical_Core); command.ExecuteNonQuery(); } } catch (SqlException ex) { } finally { if (conn.State == System.Data.ConnectionState.Open) { conn.Close(); } } } }