public void GetActualVersion() { using (Network_SchemeEntities db = new Network_SchemeEntities(DBUtils.getConnString())) { try { if (db.Network_Versions.Count() == 0) { version = 0; version_id = 0; starting_id = (version_id + 1) * 1000; } else { Network_Versions version_obj = db.Network_Versions.FirstOrDefault (p => p.ID == db.Network_Versions.Max(x => x.ID)); version = version_obj.C_Version_Number; version_id = version_obj.ID; starting_id = (version_id + 1) * 1000; } } catch { MessageBox.Show("Ошибка получения актуальной версии"); } } }
private void button2_Click(object sender, EventArgs e) { if (selected_version_id != -1) { using (Network_SchemeEntities db = new Network_SchemeEntities(DBUtils.getConnString())) { using (var transaction = db.Database.BeginTransaction()) { try { db.Network_Connections.RemoveRange(db.Network_Connections.Where(c => c.C_Version == selected_version_id)); db.Network_Hardware.RemoveRange(db.Network_Hardware.Where(c => c.C_Version == selected_version_id)); db.Network_Settings.RemoveRange(db.Network_Settings.Where(c => c.C_Version == selected_version_id)); db.Network_Ports.RemoveRange(db.Network_Ports.Where(c => c.C_Version == selected_version_id)); db.Network_Versions.RemoveRange(db.Network_Versions.Where(c => c.ID == selected_version_id)); db.SaveChanges(); transaction.Commit(); } catch (Exception ex) { MessageBox.Show("Ошибка cохранения версии : " + ex.ToString()); transaction.Rollback(); return; } } } workspace.NewScheme(); GetVersions(); } }
public void GetVersions() { label2.Text = "Версия: "; label4.Text = "Дата: "; using (Network_SchemeEntities db = new Network_SchemeEntities(DBUtils.getConnString())) { List <Network_Versions> db_versions; try { db_versions = db.Network_Versions.OrderByDescending(p => p.C_Version_Number).ToList(); } catch { MessageBox.Show("Ошибка загрузки актуальной версии"); return; } List <Control> listControls = new List <Control>(); foreach (Control control in flowLayoutPanel1.Controls) { listControls.Add(control); } foreach (Control control in listControls) { flowLayoutPanel1.Controls.Remove(control); control.Dispose(); } for (int i = 0; i < db_versions.Count; i++) { Network_Versions db_version = db_versions[i]; Button button = new Button(); button.Size = new Size(flowLayoutPanel1.Width - 6, 30); button.Tag = i; button.BackColor = SystemColors.ButtonFace; button.Text = "Версия " + db_version.C_Version_Number; flowLayoutPanel1.Controls.Add(button); button.Click += delegate { workspace.LoadDB(db_version.ID, db_version.C_Version_Number); label2.Text = "Версия: " + db_version.C_Version_Number; label4.Text = "Дата: " + db_version.C_Date; selected_version_id = db_version.ID; }; } } }
private void Form1_Load(object sender, EventArgs e) { using (Network_SchemeEntities db = new Network_SchemeEntities(DBUtils.getConnString())) { DbConnection conn = db.Database.Connection; try { conn.Open(); // проверка соединения Console.WriteLine("Connection success"); } catch // ошибка соединения { MessageBox.Show("Не удалось подключиться к серверу базы данных"); Console.WriteLine("Сonnection failed"); } } }
public void SaveToDB() { using (Network_SchemeEntities db = new Network_SchemeEntities(DBUtils.getConnString())) { Network_Versions new_version = new Network_Versions(); new_version.C_Date = DateTime.Now.ToString("d/M/yyyy"); new_version.C_Version_Number = version + 1; new_version.ID = version_id + 1; List <Network_Hardware> db_devices = new List <Network_Hardware>(); List <Network_Settings> db_settings = new List <Network_Settings>(); List <Network_Ports> db_ports = new List <Network_Ports>(); List <Network_Connections> db_connections = new List <Network_Connections>(); int connection_id = 0; foreach (NetDevice device in devices) { Network_Hardware db_device = new Network_Hardware(); db_device.ID = starting_id + device.id_real; db_device.C_Interface_X = device.sprite.Location.X; db_device.C_Interface_Y = device.sprite.Location.Y; db_device.C_Name = device.name; db_device.C_Type = device.type; db_device.C_Status = false; db_device.C_Version = new_version.ID; db_devices.Add(db_device); foreach (NetPort port in device.ports) { Network_Settings db_setting = new Network_Settings(); db_setting.ID = starting_id + port.settings.id_real; db_setting.C_IP = port.settings.IP; db_setting.C_Subnet_Mask = port.settings.subnet_mask; db_setting.C_Default_Gateway = port.settings.default_gateway; db_setting.C_Version = new_version.ID; db_settings.Add(db_setting); Network_Ports db_port = new Network_Ports(); db_port.ID = starting_id + port.id_real; db_port.C_Status = port.enabled; db_port.C_Version = new_version.ID; db_port.C_Hardware_ID = db_device.ID; db_port.C_Settings_ID = db_setting.ID; db_ports.Add(db_port); if (port.connected_port != null) { Network_Connections db_connection = new Network_Connections(); db_connection.ID = starting_id + connection_id; db_connection.C_Version = new_version.ID; db_connection.C_From_Port_ID = starting_id + port.id_real; db_connection.C_To_Port_ID = starting_id + port.connected_port.id_real; db_connections.Add(db_connection); connection_id++; } } } using (var transaction = db.Database.BeginTransaction()) { try { db.Network_Versions.Add(new_version); foreach (Network_Settings db_setting in db_settings) { db.Network_Settings.Add(db_setting); } foreach (Network_Hardware db_device in db_devices) { db.Network_Hardware.Add(db_device); } foreach (Network_Ports db_port in db_ports) { db.Network_Ports.Add(db_port); } foreach (Network_Connections db_connection in db_connections) { db.Network_Connections.Add(db_connection); } db.SaveChanges(); transaction.Commit(); } catch (Exception ex) { MessageBox.Show("Ошибка cохранения версии : " + ex.ToString()); transaction.Rollback(); } } } MessageBox.Show("Успешно сохранена версия : " + (version + 1)); GetActualVersion(); }
public void LoadDB(int load_version_id, int version_number) { GetActualVersion(); using (Network_SchemeEntities db = new Network_SchemeEntities(DBUtils.getConnString())) { List <Network_Hardware> db_devices; List <Network_Settings> db_settings; List <Network_Ports> db_ports; List <Network_Connections> db_connections; try { db_devices = db.Network_Hardware.Where(b => b.C_Version == load_version_id).ToList <Network_Hardware>(); db_settings = db.Network_Settings.Where(b => b.C_Version == load_version_id).ToList <Network_Settings>(); db_ports = db.Network_Ports.Where(b => b.C_Version == load_version_id).ToList <Network_Ports>(); db_connections = db.Network_Connections.Where(b => b.C_Version == load_version_id).ToList <Network_Connections>(); } catch { MessageBox.Show("Ошибка загрузки актуальной версии"); return; } panel.Controls.Clear(); devices.Clear(); NetDevice.id = 0; NetSettings.id = 0; NetPort.id = 0; NetDevice.pc_id = 0; NetDevice.pr_id = 0; NetDevice.sw_id = 0; NetDevice.ro_id = 0; int max_real_id = 0; int id_shift = (load_version_id) * (-1000); foreach (Network_Hardware db_device in db_devices) { int id = id_shift + db_device.ID; if (id > max_real_id) { max_real_id = id; } NetDevice device = new NetDevice(this, db_device.C_Type, false); device.x = db_device.C_Interface_X; device.y = db_device.C_Interface_Y; device.name = db_device.C_Name; device.title.Text = device.name; device.id_real = id; devices.Add(device); } NetDevice.id = max_real_id + 1; max_real_id = 0; foreach (Network_Ports db_port in db_ports) { int id = id_shift + db_port.ID; if (id > max_real_id) { max_real_id = id; } NetPort port = new NetPort(null); port.id_real = id; port.enabled = db_port.C_Status; port.settings_id = id_shift + db_port.C_Settings_ID; // set device int device_id = id_shift + db_port.C_Hardware_ID; foreach (NetDevice device in devices) { if (device.id_real == device_id) { port.device = device; for (int i = 0; i < device.ports.Count; i++) { if (device.ports[i] == null) { device.ports[i] = port; break; } } break; } } } NetPort.id = max_real_id + 1; max_real_id = 0; foreach (Network_Settings db_setting in db_settings) { int id = id_shift + db_setting.ID; if (id > max_real_id) { max_real_id = id; } NetSettings setting = new NetSettings(); setting.id_real = id; setting.IP = db_setting.C_IP; setting.default_gateway = db_setting.C_Default_Gateway; setting.subnet_mask = db_setting.C_Subnet_Mask; // set device bool stop = false; foreach (NetDevice device in devices) { if (stop) { break; } foreach (NetPort port in device.ports) { if (setting.id_real == port.settings_id) { port.settings = setting; stop = true; break; } } } } NetSettings.id = max_real_id + 1; foreach (Network_Connections db_connection in db_connections) { int port_start_id = id_shift + db_connection.C_From_Port_ID; int port_end_id = id_shift + db_connection.C_To_Port_ID; NetPort start_port = null; NetPort end_port = null; bool stop = false; foreach (NetDevice device in devices) { if (stop) { break; } foreach (NetPort port in device.ports) { if (port.id_real == port_start_id) { start_port = port; stop = true; break; } } } stop = false; foreach (NetDevice device in devices) { if (stop) { break; } foreach (NetPort port in device.ports) { if (port.id_real == port_end_id) { end_port = port; stop = true; break; } } } start_port.connected_port = end_port; end_port.connected_port = start_port; } } RefreshDraw(); panel.Refresh(); MessageBox.Show("Успешно загружена версия : " + version_number); }