база данных — основная тема материала.
registration.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace Kurs2
{
public partial class registration : Form
{
public Boolean checkRole()
{
if (btnStudent.BackColor == Color.Gray)
{
return true;
}
else
{
return false;
}
}
private void loadComboBox()
{
string sql = $»select Код_группы from Группа»;
using (SqlCommand cmd = new SqlCommand(sql, dataBase.GetConnection()))
{
cmd.CommandType = CommandType.Text;
DataTable tbl = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(tbl);
groupComboBox.DisplayMember = «Код_группы»;
groupComboBox.DataSource = tbl;
}
}
private void loadjobComboBox()
{
string sql = $»select Код_должности, Название_должности from Должности»;
using (SqlCommand cmd = new SqlCommand(sql, dataBase.GetConnection()))
{
cmd.CommandType = CommandType.Text;
DataTable tbl = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(tbl);
groupComboBox.DisplayMember = «Код_группы»;
groupComboBox.DataSource = tbl;
}
}
private void loadjobComboBox()
{
string sql = $»select Код_должности, Название_должности from Должности»;
using (SqlCommand cmd = new SqlCommand(sql, dataBase.GetConnection()))
{
cmd.CommandType = CommandType.Text;
DataTable tbl = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(tbl);
jobComboBox.ValueMember = «Код_должности»;
jobComboBox.DisplayMember = «Название_должности»;
jobComboBox.DataSource = tbl;
}
}
DataBase dataBase = new DataBase();
public registration()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
//614; 567 ; 534; 401
}
private void label1_Click(object sender, EventArgs e)
{
}
private void registration_Load(object sender, EventArgs e)
{
textBox2.PasswordChar = ‘•’;
pictureBox2.Visible = false;
textBox1.MaxLength = 50;
textBox2.MaxLength = 50;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != «» && textBox2.Text != «» && firstNametextBox.Text != «» && secondNametextBox.Text != «» && surNametextBox.Text != «»)
{
if (btn.BackColor != Color.White || btnStudent.BackColor != Color.White)
{
if (!checkUser())
{
var loginUser = textBox1.Text;
var passUser = textBox2.Text;
var check = checkRole();
var firstName = firstNametextBox.Text;
var secondName = secondNametextBox.Text;
var surName = surNametextBox.Text;
var numberOfKurs = KursTextBox.Text;
int role;
if (check)
{
role = 1;
}
else
{
role = 2;
}
dataBase.openConnection();
if (role == 1)
{
if (KursTextBox.Text != «»)
{
try
{
string querystringpeople = $»insert into Физ_лицо(Фамилия, Имя, Отчество) OUTPUT INSERTED.id_физ_лица values(‘{secondName}’, ‘{firstName}’,'{surName}’)»;
SqlCommand command2 = new SqlCommand(querystringpeople, dataBase.GetConnection());
var result = command2.ExecuteScalar();
var item = groupComboBox.Text;
string querystringsrudent = $»insert into Студенты(id_физ_лица, Курс, Код_группы) values(‘{result}’,'{numberOfKurs}’, ‘{item}’) «;
SqlCommand cmd3 = new SqlCommand(querystringsrudent, dataBase.GetConnection());
if (cmd3.ExecuteNonQuery() == 1)
{
MessageBox.Show(«Студент успешно создан», «Успешно», MessageBoxButtons.OK, MessageBoxIcon.Information);
string querystring2 = $»insert into Регистрация(id_физического_лица,Логин, Пароль_польз, Роль) values(‘{result}’,'{loginUser}’, ‘{passUser}’, ‘{role}’)»;
SqlCommand command = new SqlCommand(querystring2, dataBase.GetConnection());
if (command.ExecuteNonQuery() == 1)
{
MessageBox.Show(«Акаунт успешно создан», «Успешно», MessageBoxButtons.OK, MessageBoxIcon.Information);
Form1 frm1 = new Form1();
this.Hide();
frm1.ShowDialog();
}
if (command.ExecuteNonQuery() != 0)
{
MessageBox.Show(«Ошибка», «Ошибка», MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
if (cmd3.ExecuteNonQuery() != 0)
{
MessageBox.Show(«Ошибка», «Ошибка», MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch(Exception)
{
MessageBox.Show(«Ошибка», «Ошибка», MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show(«Введите номер курса», «Ошибка», MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
var itemJob = Convert.ToInt32(jobComboBox.SelectedValue);
if (itemJob != 0)
{
string querystringpeople = $»insert into Физ_лицо(Фамилия, Имя, Отчество) OUTPUT INSERTED.id_физ_лица values(‘{secondName}’, ‘{firstName}’,'{surName}’)»;
SqlCommand command2 = new SqlCommand(querystringpeople, dataBase.GetConnection());
var result = command2.ExecuteScalar();
var item = jobComboBox.SelectedValue;
string querystringteacher = $»insert into Преподаватели(id_физ_лица, Код_должности) values(‘{result}’,'{item}’) «;
SqlCommand cmdJob = new SqlCommand(querystringteacher, dataBase.GetConnection());
if (cmdJob.ExecuteNonQuery() == 1)
{
MessageBox.Show(«Преподаватель успешно добавлен», «Успешно», MessageBoxButtons.OK, MessageBoxIcon.Information);
string querystring2 = $»insert into Регистрация(id_физического_лица,Логин, Пароль_польз, Роль) values(‘{result}’,'{loginUser}’, ‘{passUser}’, ‘{role}’)»;
SqlCommand command = new SqlCommand(querystring2, dataBase.GetConnection());
command.ExecuteNonQuery();
Form1 frm1 = new Form1();
this.Hide();
frm1.ShowDialog();
}
}
else
{
MessageBox.Show(«Выберите должность», «Ошибка», MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
dataBase.closeConnection();
}
}
else
{
MessageBox.Show(«Выберите роль», «Ошибка», MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show(«Поля не могут быть пустые», «Ошибка», MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private Boolean checkUser()
{
var loginUser = textBox1.Text;
var passUser = textBox2.Text;
var firstName = firstNametextBox.Text;
var secondName = secondNametextBox.Text;
var surName = surNametextBox.Text;
SqlDataAdapter adapter = new SqlDataAdapter();
DataTable table = new DataTable();
string querystring = $»select id_пользователя, Логин, Пароль_польз from Регистрация where Логин = ‘{loginUser}’ and Пароль_польз = ‘{passUser}’ «;
SqlCommand command = new SqlCommand(querystring, dataBase.GetConnection());
adapter.SelectCommand = command;
adapter.Fill(table);
if (table.Rows.Count > 0)
{
MessageBox.Show(«Пользователь уже существует!», «Ошибка», MessageBoxButtons.OK, MessageBoxIcon.Warning);
return true;
}
else
{
SqlDataAdapter adapterforstudents = new SqlDataAdapter();
DataTable tableforstudents = new DataTable();
string querystringstudents = $»select id_физ_лица, Фамилия, Имя, Отчество from Физ_лицо where Фамилия = ‘{secondName}’ and Имя = ‘{firstName}’ and Отчество = ‘{surName}’ «;
SqlCommand commandStudents = new SqlCommand(querystringstudents, dataBase.GetConnection());
adapterforstudents.SelectCommand = commandStudents;
adapterforstudents.Fill(tableforstudents);
if (tableforstudents.Rows.Count > 0)
{
MessageBox.Show(«Пользователь уже существует!», «Ошибка», MessageBoxButtons.OK, MessageBoxIcon.Warning);
return true;
}
else
{
return false;
}
}
}
private void pictureBox3_Click(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = true;
pictureBox2.Visible = true;
pictureBox3.Visible = false;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = false;
pictureBox2.Visible = false;
pictureBox3.Visible = true;
}
private void firstNameLabel_Click(object sender, EventArgs e)
{
}
private void btnStudent_Click(object sender, EventArgs e)
{
btnStudent.BackColor = Color.Gray;
btn.BackColor = Color.White;
panel2.Size = new Size(534, 493);
Size = new Size(614, 659);
button1.Location = new Point(197, 437);
KursNameLabel.Visible = true;
KursTextBox.Visible = true;
GroupLabel.Visible = true;
groupComboBox.Visible = true;
jobLabel.Visible = false;
jobComboBox.Visible = false;
}
private void btn_Click(object sender, EventArgs e)
{
btn.BackColor = Color.Gray;
btnStudent.BackColor = Color.White;
panel2.Size = new Size(534, 470);
Size = new Size(614, 628);
button1.Location = new Point(197, 407);
KursNameLabel.Visible = false;
KursTextBox.Visible = false;
GroupLabel.Visible = false;
