Repository navigation
Expand file tree
/
Copy pathFormAddClient.cs
More file actions
134 lines (110 loc) · 4.62 KB
/
Copy pathFormAddClient.cs
File metadata and controls
134 lines (110 loc) · 4.62 KB
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
using MySql.Data.MySqlClient;
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;
namespace ProjectAvtoMoika
{
public partial class AddClient : Form
{
public AddClient()
{
InitializeComponent();
LoginField.Text = "Введите имя:";
Nums.Text = "Введите номер авто:";
NumberMob.Text = "Введите телефон:";
}
private void button1_Click(object sender, EventArgs e)
{
if (LoginField.Text == "Введите имя:")
{
MessageBox.Show("Введите Имя!");
return;
}
if (Nums.Text == "Введите номер авто:")
{
MessageBox.Show("Введите Номер авто!");
return;
}
if (NumberMob.Text == "Введите телефон:")
{
MessageBox.Show("Введите номер телефона!");
return;
}
bool userExists = checkUser();
if (checkUser())
{
DB dB = new DB();
MySqlCommand command = new MySqlCommand("INSERT INTO `clients` (`Nums`, `Client`,`NumsMob`) VALUES (@Nums,@Client,@NumsMob);", dB.GetConnection());
command.Parameters.Add("@Nums", MySqlDbType.VarChar).Value = Nums.Text;
command.Parameters.Add("@Client", MySqlDbType.VarChar).Value = LoginField.Text;
command.Parameters.Add("@NumsMob", MySqlDbType.VarChar).Value = NumberMob.Text;
dB.OpenCon();
if (command.ExecuteNonQuery() == 1)
{
//получение ID последнего вставленного клиента
long newClientId = command.LastInsertedId;
MessageBox.Show($"Клиент добавлен,его id: (ID: {newClientId})");
}
else
MessageBox.Show("Клиент не был добавлен");
dB.CloseCon();
}
}
private void LoginField_Enter(object sender, EventArgs e)
{
if (LoginField.Text == "Введите имя:")
LoginField.Text = "";
}
private void LoginField_Leave(object sender, EventArgs e)
{
if (LoginField.Text == "")
LoginField.Text = "Введите имя:";
}
private void Nums_Enter(object sender, EventArgs e)
{
if (Nums.Text == "Введите номер авто:")
Nums.Text = "";
}
private void Nums_Leave(object sender, EventArgs e)
{
if (Nums.Text == "")
Nums.Text = "Введите номер авто:";
}
//метод для проверки существования пользователя в базе данных
public Boolean checkUser()
{
DB db = new DB();
DataTable dataTable = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM `clients` WHERE `Client` = @ul AND `Nums` = @nomer AND `NumsMob` = @tel ", db.GetConnection());
cmd.Parameters.Add("@ul", MySqlDbType.VarChar).Value = LoginField.Text;
cmd.Parameters.Add("@nomer", MySqlDbType.VarChar).Value = Nums.Text;
cmd.Parameters.Add("@tel", MySqlDbType.Int64).Value = NumberMob.Text;
adapter.SelectCommand = cmd;
adapter.Fill(dataTable); //Заполнение таблицы результатами запроса
if (dataTable.Rows.Count > 0)
{
int clientId = Convert.ToInt32(dataTable.Rows[0]["id"]);
MessageBox.Show($"Такой логин и номер уже есть, но вы можете продолжить.\nЕго id: (ID: {clientId})");
return false;
}
return true;
}
private void NumberMob_Enter(object sender, EventArgs e)
{
if (NumberMob.Text == "Введите телефон:")
NumberMob.Text = "";
}
private void NumberMob_Leave(object sender, EventArgs e)
{
if (NumberMob.Text == "")
NumberMob.Text = "Введите телефон:";
}
}
}