using System using System Collections Generic using System ComponentMo

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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 MySql.Data.MySqlClient;
namespace Database
{
public partial class Form3 : Form
{
public string nameUser = "";
MySqlConnection connection = new MySqlConnection();
MySqlCommand command = new MySqlCommand();
MySqlDataReader reader;
DataTable table;
public Form3()
{
InitializeComponent();
this.Text += nameUser;
connection.ConnectionString = "Server=localhost;database=lab;username=root;password=GreblyA131GreblyA";
command.Connection = connection;
try
{
connection.Open();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Ошибка при подключении к серверу", MessageBoxButtons.OK, MessageBoxIcon.Error);
//закрыть приложение
}
}
private void Form3_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (reader != null) reader.Close();
connection.Close();
Application.Exit();
}
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (toolStripComboBox1.SelectedIndex)
{
case 0:
command.CommandText = "SELECT id_center AS Код_центра, name AS Название, address AS Адрес, telephone AS Телефон, email AS Email " +
"FROM fitness_center";
break;
case 1:
command.CommandText = "SELECT id_type AS Код_типа, name AS Наименование " +
"FROM type_gym";
break;
case 2:
command.CommandText = "SELECT id_gym AS Код_зала, type_gym.name AS Тип, area AS Площадь, description AS Описание, fitness_center.name AS Фитнесс_центер " +
"FROM gym, type_gym, fitness_center " +
"WHERE type = id_type AND gym.id_center = fitness_center.id_center " +
"ORDER BY id_gym";
break;
case 3:
command.CommandText = "SELECT id_instructor AS Код_инструктора, surname AS Фамилия, instructor.name AS Имя, second_name AS Отчество, age AS Возраст, " +
"work_record AS Стаж, education AS Образование, salary AS Зарплата, fitness_center.name AS Фитнесс_центер " +
"FROM instructor, fitness_center " +
"WHERE instructor.id_center = fitness_center.id_center " +
"ORDER BY id_instructor";
break;
case 4:
command.CommandText = "SELECT id_type AS Код_типа, name AS Наименование " +
"FROM type_subscription";
break;
case 5:
command.CommandText = "SELECT id_subscription AS Код_абонемента, type_subscription.name AS Тип, type_gym.name AS Зал, fitness_center.name Фитнесс_центер, price AS Цена " +
"FROM subscription, type_subscription, gym, type_gym, fitness_center " +
"WHERE subscription.type = type_subscription.id_type AND subscription.id_gym = gym.id_gym AND gym.type = type_gym.id_type AND gym.id_center = fitness_center.id_center " +
"ORDER BY id_subscription";
break;
case 6:
command.CommandText = "SELECT id_client AS Номер_клиента, surname AS Фамилия, name AS Имя, second_name AS Отчество, telephone AS Телефон " +
"FROM client";
break;
case 7:
command.CommandText = "SELECT id_client AS Код_клиента, id_subscription AS Код_абонемента, id_instructor AS Код_интруктора, begin AS Дата_начала, end AS Дата_окончания " +
"FROM visit";
break;
case 8:
command.CommandText = "SELECT id_sport AS Код_спорта, type AS Вид_спорта " +
"FROM sport";
break;
case 9:
command.CommandText = "SELECT id_training AS Код_тренировки, sport.type AS Вид_спорта, day_of_week AS День_недели, time AS Начало_занятия, CONCAT_WS(\" \", instructor.surname, instructor.name, instructor.second_name) AS Инструктор, type_gym.name AS Зал, fitness_center.name AS Фитнесс_центер " +
"FROM training, sport, gym, type_gym, instructor, fitness_center " +
"WHERE training.sport = sport.id_sport AND training.id_gym = gym.id_gym AND type_gym.id_type = gym.type AND gym.id_center = fitness_center.id_center AND training.id_instructor = instructor.id_instructor " +
"ORDER BY id_training";
break;
}
reader = command.ExecuteReader();
table = new DataTable();
table.Load(reader);
bindingSource1.DataSource = table;
dataGridView1.DataSource = bindingSource1;
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(richTextBox1.Text))
{
command.CommandText = richTextBox1.Text;
try
{
reader = command.ExecuteReader();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, "Ошибка в тексте запроса", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
table = new DataTable();
table.Load(reader);
bindingSource1.DataSource = table;
dataGridView1.DataSource = bindingSource1;
}
}
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
}
}
}