123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace ЭлектронныйЖурнал
- {
- public partial class ДобавитьПол : Form
- {
- SqlConnection connection;
- SqlCommand command;
- SqlDataAdapter adapter;
- DataTable table;
- string conn = "Server=DESKTOP-JES5T51\\SQLEXPRESS;DataBase=EJBD;Trusted_connection=true";
- public ДобавитьПол()
- {
- InitializeComponent();
- connection = new SqlConnection("Server=DESKTOP-JES5T51\\SQLEXPRESS;DataBase=EJBD;Trusted_connection=true");
- command = new SqlCommand();
- command.Connection = connection;
- command.CommandText = Text;
- adapter = new SqlDataAdapter(command);
- table = new DataTable();
- }
- private void ShowTable(string text)
- {
- dataGridView1.Columns.Clear();
- dataGridView1.DataSource = null;
- command.CommandText = text;
- table.Clear();
- adapter.Fill(table);
- dataGridView1.DataSource = table;
- }
- private void Пользователи_Load(object sender, EventArgs e)
- {
- ShowTable("Select Login as Логин, Password as Пароль from Пользователи");
- }
- private void AddButton_Click(object sender, EventArgs e)
- {
- {
- if (string.IsNullOrEmpty(LoginB.Text) || string.IsNullOrEmpty(PassB.Text))
- {
- MessageBox.Show("Не все поля заполнен!");
- return;
- }
- else
- {
- if (MessageBox.Show("Добавить нового пользователя?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- try
- {
- connection.Open();
- command.CommandText = "INSERT INTO Пользователи (Login, Password) VALUES(@l,@p)";
- command.Parameters.AddWithValue("@l", LoginB.Text);
- command.Parameters.AddWithValue("@p", PassB.Text);
- command.ExecuteReader();
- command.Parameters.Clear();
- connection.Close();
- MessageBox.Show("Новый пользователь был добавлен!");
- ShowTable("Select Login as Логин, Password as Пароль from Пользователи");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- command.Parameters.Clear();
- connection.Close();
- }
- }
- }
- }
- }
- private void DeleteButton_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("вы хотите удалить?", "Ошибка", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- connection.Open();
- command.CommandText = ("Delete From Пользователи Where Login=@l");
- command.Parameters.AddWithValue("@l", dataGridView1.CurrentRow.Cells[0].Value);
- command.ExecuteReader();
- command.Parameters.Clear();
- connection.Close();
- MessageBox.Show("Запись была удалена");
- ShowTable("Select Login as Логин, Password as Пароль from Пользователи");
- }
- }
- private void Back_Click(object sender, EventArgs e)
- {
- Menu i = new Menu();
- i.Show();
- this.Hide();
- }
- }
- }
|