private void button1_Click(object sender, EventArgs e) { DBUtils.setLoginPassword(textBox1.Text, textBox2.Text); // using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString())) { DbConnection conn = db.Database.Connection; try { conn.Open(); // проверка соединения Console.WriteLine("Connection success"); this.Hide(); var form1 = new MenuForm(); form1.Closed += (s, args) => this.Close(); form1.Show(); } catch // ошибка соединения { Console.WriteLine("Сonnection failed"); MessageBox.Show("Неправильная пара логин-пароль"); textBox2.Text = ""; } } }
public void RefreshData() { using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString())) { try { db.UpdateSessionStatus(); } catch { MessageBox.Show("У вас нет прав для обновления статуса заседаний"); } try { menu.zasedaniyaData = db.Zasedanies.Select(p => new { p.Nomer, p.Naimenovanie, p.Tip, p.Nomer_sozyva, p.Data_provedeniya, p.Vremya_nachala, p.Vremya_konca, p.Status_zasedaniya, p.Mesto_provedeniya1 }).ToList(); } catch { MessageBox.Show("У вас нет прав для просмотра таблицы"); } } dataGridView1.DataSource = menu.zasedaniyaData; }
//get student information: MASV, HO, TEN, NGAYSINH, NAM, MAKH public static DataSet GetStudentInformation() { SqlConnection scon = DBUtils.GetDBConnection(); String sqlStr = "SELECT MASV, HO, TEN, NGAYSINH, GIOITINH, MAKH FROM SVIEN"; try { scon.Open(); SqlDataAdapter daStudent = new SqlDataAdapter(sqlStr, scon); DataSet dsStudent = new DataSet(); daStudent.Fill(dsStudent); return(dsStudent); } catch (Exception) { MessageBox.Show("Open Connection error"); } finally { scon.Close(); } return(null); }
private void fillInputs() { SqlConnection conn = DBUtils.GetDBConnection(); string sqlExpression = "SELECT doctor, vistime_begin, vistime_end, visit_date, diagnosis, service FROM appointments WHERE diagnosis=@diagnosis"; try { conn.Open(); SqlCommand command = new SqlCommand(sqlExpression, conn); command.Parameters.AddWithValue("@diagnosis", searchDiagnosis.Text); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { int doc_id = Convert.ToInt32(reader.GetValue(0)); getDoctor(doc_id); string workBegin = DateTime.Parse($"{reader.GetValue(1)}").ToString("t"); string workEnd = DateTime.Parse($"{reader.GetValue(2)}").ToString("t"); labelTime.Text = $"{workBegin}-{workEnd}"; labelDate.Text = DateTime.Parse($"{reader.GetValue(3)}").ToString("D"); labelDiagnosis.Text = $"{reader.GetValue(4)}"; int serv = reader.GetInt32(5); getService(serv); } } } catch (Exception ex) { MessageBox.Show("Произошла ошибка при заполнении", "Ошибка", MessageBoxButtons.OK); } finally { conn.Close(); } }
private void button3_Click(object sender, EventArgs e) { using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString())) { if (dataGridView1.SelectedRows.Count < 1) { return; } int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (converted == false) { return; } Frakciya frak = db.Frakciyas.Find(id); FrakciyaForm addForm = new FrakciyaForm(); addForm.textBox1.Text = frak.Naimenovanie; List <Deputaty> deps = db.Deputaties.ToList(); for (int i = deps.Count - 1; i >= 0; i--) { if (deps[i].ID_frakcii != id) { deps.RemoveAt(i); } } addForm.comboBox1.DataSource = deps; addForm.comboBox1.ValueMember = "Partiyniy_bilet"; addForm.comboBox1.DisplayMember = "Familiya"; addForm.comboBox1.SelectedItem = frak.Lider_frakcii; DialogResult result = addForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } frak.Naimenovanie = addForm.textBox1.Text; if (addForm.comboBox1.SelectedItem != null) { frak.Deputaty = (Deputaty)addForm.comboBox1.SelectedItem; } else { frak.Deputaty = null; } if (addForm.comboBox1.SelectedValue != null) { frak.Lider_frakcii = (int)addForm.comboBox1.SelectedValue; } else { frak.Lider_frakcii = null; } using (var transaction = db.Database.BeginTransaction()) { try { db.Entry(frak).State = EntityState.Modified; try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show("Ошибка обновления : " + ex.ToString()); } transaction.Commit(); } catch (Exception ex) { MessageBox.Show("Ошибка обновления : " + ex.ToString()); try { transaction.Rollback(); } catch (Exception ex2) { MessageBox.Show("Ошибка отката : " + ex2.ToString()); } } } RefreshData(); } }
private void button3_Click(object sender, EventArgs e) { using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString())) { if (dataGridView1.SelectedRows.Count < 1) { return; } int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (converted == false) { return; } Zakonoproekt zak = db.Zakonoproekts.Find(id); ZakonoproektForm addForm = new ZakonoproektForm(); addForm.textBox1.Text = zak.Naimenovanie; addForm.richTextBox1.Text = zak.Opisanie; List <Deputaty> deps = db.Deputaties.ToList(); addForm.comboBox1.DataSource = deps; addForm.comboBox1.ValueMember = "Partiyniy_bilet"; addForm.comboBox1.DisplayMember = "Familiya"; addForm.comboBox1.SelectedItem = zak.Deputaty; DialogResult result = addForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } zak.Naimenovanie = addForm.textBox1.Text; zak.Opisanie = addForm.richTextBox1.Text; zak.Deputaty = (Deputaty)addForm.comboBox1.SelectedItem; zak.Vnesen = (int)addForm.comboBox1.SelectedValue; using (var transaction = db.Database.BeginTransaction()) { try { db.Entry(zak).State = EntityState.Modified; try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show("Ошибка обновления : " + ex.ToString()); } transaction.Commit(); } catch (Exception ex) { MessageBox.Show("Ошибка обновления : " + ex.ToString()); try { transaction.Rollback(); } catch (Exception ex2) { MessageBox.Show("Ошибка отката : " + ex2.ToString()); } } } RefreshData(); } }
private void button2_Click(object sender, EventArgs e) { using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString())) { ZasedaniyaForm addForm = new ZasedaniyaForm(); List <Sozyvy> sozes = db.Sozyvies.ToList(); addForm.comboBox1.DataSource = sozes; addForm.comboBox1.ValueMember = "Nomer"; addForm.comboBox1.DisplayMember = "Nomer"; List <Mesto_provedeniya> mesta = db.Mesto_provedeniya.ToList(); addForm.comboBox2.DataSource = mesta; addForm.comboBox2.ValueMember = "ID"; addForm.comboBox2.DisplayMember = "Ulica"; addForm.radioButton1.Checked = true; addForm.radioButton2.Checked = false; DialogResult result = addForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } Zasedanie zased = new Zasedanie(); zased.Nomer = 0; zased.Naimenovanie = addForm.textBox1.Text; zased.Data_provedeniya = DateTime.Parse(addForm.maskedTextBox1.Text); zased.Vremya_nachala = TimeSpan.Parse(addForm.maskedTextBox2.Text); zased.Vremya_konca = TimeSpan.Parse(addForm.maskedTextBox3.Text); if (addForm.radioButton1.Checked) { zased.Tip = addForm.radioButton1.Text; } else { zased.Tip = addForm.radioButton2.Text; } zased.Nomer_sozyva = (int)addForm.comboBox1.SelectedValue; zased.Sozyvy = (Sozyvy)addForm.comboBox1.SelectedItem; zased.Mesto_provedeniya = (int)addForm.comboBox2.SelectedValue; zased.Mesto_provedeniya1 = (Mesto_provedeniya)addForm.comboBox2.SelectedItem; using (var transaction = db.Database.BeginTransaction()) { try { db.Zasedanies.Add(zased); try { db.SaveChanges(); } catch (DbUpdateException ex) { MessageBox.Show("Ошибка добавления : " + ex.ToString()); } transaction.Commit(); } catch (Exception ex) { MessageBox.Show("Ошибка добавления : " + ex.ToString()); try { transaction.Rollback(); } catch (Exception ex2) { MessageBox.Show("Ошибка отката : " + ex2.ToString()); } } } RefreshData(); } }
private void button3_Click(object sender, EventArgs e) { using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString())) { if (dataGridView1.SelectedRows.Count < 1) { return; } int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (converted == false) { return; } Zasedanie zased = db.Zasedanies.Find(id); ZasedaniyaForm addForm = new ZasedaniyaForm(); addForm.textBox1.Text = zased.Naimenovanie; addForm.maskedTextBox1.Text = zased.Data_provedeniya.ToString(); addForm.maskedTextBox2.Text = zased.Vremya_nachala.ToString(); addForm.maskedTextBox3.Text = zased.Vremya_konca.ToString(); List <Sozyvy> sozes = db.Sozyvies.ToList(); addForm.comboBox1.DataSource = sozes; addForm.comboBox1.ValueMember = "Nomer"; addForm.comboBox1.DisplayMember = "Nomer"; List <Mesto_provedeniya> mesta = db.Mesto_provedeniya.ToList(); addForm.comboBox2.DataSource = mesta; addForm.comboBox2.ValueMember = "ID"; addForm.comboBox2.DisplayMember = "Ulica"; addForm.comboBox1.SelectedItem = zased.Sozyvy; addForm.comboBox2.SelectedItem = zased.Mesto_provedeniya1; if (zased.Tip == "Plenarnoe Zasedanie" || zased.Tip == "Пленарное заседание") { addForm.radioButton1.Checked = true; } else if (zased.Tip == "Заседание совета ГД") { addForm.radioButton2.Checked = true; } DialogResult result = addForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } zased.Naimenovanie = addForm.textBox1.Text; zased.Data_provedeniya = DateTime.Parse(addForm.maskedTextBox1.Text); zased.Vremya_nachala = TimeSpan.Parse(addForm.maskedTextBox2.Text); zased.Vremya_konca = TimeSpan.Parse(addForm.maskedTextBox3.Text); if (addForm.radioButton1.Checked) { zased.Tip = addForm.radioButton1.Text; } else { zased.Tip = addForm.radioButton2.Text; } zased.Nomer_sozyva = (int)addForm.comboBox1.SelectedValue; zased.Sozyvy = (Sozyvy)addForm.comboBox1.SelectedItem; zased.Mesto_provedeniya = (int)addForm.comboBox2.SelectedValue; zased.Mesto_provedeniya1 = (Mesto_provedeniya)addForm.comboBox2.SelectedItem; using (var transaction = db.Database.BeginTransaction()) { try { db.Entry(zased).State = EntityState.Modified; try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show("Ошибка обновления : " + ex.ToString()); } transaction.Commit(); } catch (Exception ex) { MessageBox.Show("Ошибка обновления : " + ex.ToString()); try { transaction.Rollback(); } catch (Exception ex2) { MessageBox.Show("Ошибка отката : " + ex2.ToString()); } } } RefreshData(); } }
private void enterButton_Click(object sender, EventArgs e) // при нажатии на кнопку ВОЙТИ { if (loginTextBox.Text == "" || passTextBox.Text == "") // заполнение строк логина и пароля обязательно { MessageBox.Show("Заполните пустые строки!", "Ошибка", MessageBoxButtons.OK); } else { SqlConnection conn = DBUtils.GetDBConnection(); string sqlExpression = $"SELECT * FROM users WHERE login = '******' AND password = '******'"; int user_id = 0; try { conn.Open(); SqlCommand command = new SqlCommand(sqlExpression, conn); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) // если пользователь найден { int userRole = getUserData(reader, ref user_id); // проверяем его роль и доступ if (userRole == Constants.ACCESS_ERROR) { MessageBox.Show("Доступ запрещен.", "Ошибка", MessageBoxButtons.OK); } else if (userRole == Constants.ADMIN_ROLE) { clearInputs(); AdminForm adminForm = new AdminForm(); adminForm.Show(); this.Hide(); } else if (userRole == Constants.USER_ROLE) { clearInputs(); CurrentUser.Id = user_id; //сохраняем id текущего пользователя UserForm userForm = new UserForm(); userForm.Show(); this.Hide(); } else { clearInputs(); CurrentUser.Id = user_id; //сохраняем id текущего пользователя DocForm docForm = new DocForm(); docForm.Show(); this.Hide(); } } else { MessageBox.Show("Такого пользователя не существует. Введите данные снова.", "Ошибка", MessageBoxButtons.OK); } } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK); } finally { // Гарантировать освобождение подключения conn.Close(); } } }
private void button3_Click(object sender, EventArgs e) { using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString())) { if (dataGridView1.SelectedRows.Count < 1) { return; } int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (converted == false) { return; } Sozyvy sozyv = db.Sozyvies.Find(id); SozyvForm addForm = new SozyvForm(); addForm.maskedTextBox1.Text = sozyv.Data_nachala.ToString(); addForm.maskedTextBox2.Text = sozyv.Data_konca.ToString(); // получаем список команд List <Deputaty> deps = db.Deputaties.ToList(); addForm.listBox1.DataSource = deps; addForm.listBox1.ValueMember = "Partiyniy_bilet"; addForm.listBox1.DisplayMember = "Familiya"; foreach (Deputaty d in sozyv.Deputaties) { addForm.listBox1.SelectedItem = d; } DialogResult result = addForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } sozyv.Data_nachala = DateTime.Parse(addForm.maskedTextBox1.Text); sozyv.Data_konca = DateTime.Parse(addForm.maskedTextBox2.Text); deps.Clear(); // очищаем список и заново заполняем его выделенными элементами foreach (var dep in addForm.listBox1.SelectedItems) { deps.Add((Deputaty)dep); } sozyv.Deputaties = deps; foreach (var d in deps) { if (addForm.listBox1.SelectedItems.Contains(d)) { if (!sozyv.Deputaties.Contains(d)) { sozyv.Deputaties.Add(d); } } else { if (sozyv.Deputaties.Contains(d)) { sozyv.Deputaties.Remove(d); } } } using (var transaction = db.Database.BeginTransaction()) { try { db.Entry(sozyv).State = EntityState.Modified; try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show("Ошибка обновления : " + ex.ToString()); } transaction.Commit(); } catch (Exception ex) { MessageBox.Show("Ошибка обновления : " + ex.ToString()); try { transaction.Rollback(); } catch (Exception ex2) { MessageBox.Show("Ошибка отката : " + ex2.ToString()); } } } RefreshData(); } }