1
| using System;
|
2
| using System.IO;
|
3
| using System.Collections.Generic;
|
4
| using System.ComponentModel;
|
5
| using System.Data;
|
6
| using System.Data.SqlClient;
|
7
| using System.Drawing;
|
8
| using System.Linq;
|
9
| using System.Text;
|
10
| using System.Threading.Tasks;
|
11
| using System.Windows.Forms;
|
12
|
|
13
| namespace testWork
|
14
| {
|
15
| public partial class Form_update_employee : Form
|
16
| {
|
17
| SqlConnection sqlConn;
|
18
| string FileName = "",
|
19
| id = "";
|
20
| byte[] imageData;
|
21
|
|
22
| public Form_update_employee(SqlConnection sqlConn, string id)
|
23
| {
|
24
| InitializeComponent();
|
25
|
|
26
| this.sqlConn = sqlConn;
|
27
| this.id = id;
|
28
| }
|
29
|
|
30
| private void Form_update_employee_Load(object sender, EventArgs e)
|
31
| {
|
32
| SqlDataReader sqlRead = null;//перемеена для хранение вывода запроса
|
33
|
|
34
| SqlCommand comend = new SqlCommand();//перемеена для хранение запроса
|
35
|
|
36
|
|
37
| string comond = "SELECT *" +
|
38
| "FROM [employee]" +
|
39
| "WHERE [id] = @id ";
|
40
| comend = new SqlCommand(comond, sqlConn);
|
41
| comend.Parameters.AddWithValue("@id", Convert.ToString(id));
|
42
|
|
43
|
|
44
|
|
45
| if (sqlRead != null)
|
46
| sqlRead.Close();//проверка на откратасть
|
47
|
|
48
| try
|
49
| {
|
50
| sqlRead = comend.ExecuteReader();//создаем запрос
|
51
|
|
52
|
|
53
| while (sqlRead.Read())
|
54
| {
|
55
| surname.Text = Convert.ToString(sqlRead["surname"]);
|
56
| name.Text = Convert.ToString(sqlRead["name"]);
|
57
| patronymic.Text = Convert.ToString(sqlRead["patronymic"]);
|
58
| specialty.Text = Convert.ToString(sqlRead["specialty"]);
|
59
| login.Text = Convert.ToString(sqlRead["login"]);
|
60
| password.Text = Convert.ToString(sqlRead["password"]);
|
61
| dostoop.Text = Convert.ToString(sqlRead["dostoop"]);
|
62
|
|
63
| try
|
64
| {
|
65
| byte[] data = (byte[])sqlRead["image"];
|
66
| System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
|
67
| Image img = System.Drawing.Image.FromStream(ms);
|
68
| pictureBox1.Image = new Bitmap(img, new Size(100, 100));
|
69
| }
|
70
| catch
|
71
| {
|
72
|
|
73
| }
|
74
| }
|
75
| }
|
76
| catch (Exception ex)//обработка исключений
|
77
| {
|
78
| MessageBox.Show(ex.Message.ToString(), ex.Source.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
|
79
| }
|
80
| finally
|
81
| {
|
82
| if (sqlRead != null)
|
83
| sqlRead.Close();//проверка на откратасть
|
84
|
|
85
|
|
86
| }
|
87
| }
|
88
|
|
89
| private void button_photo_Click(object sender, EventArgs e)
|
90
| {
|
91
| // диалог для выбора файла
|
92
| OpenFileDialog ofd = new OpenFileDialog();
|
93
| // фильтр форматов файлов
|
94
| ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*";
|
95
| // если в диалоге была нажата кнопка ОК
|
96
| if (ofd.ShowDialog() == DialogResult.OK)
|
97
| {
|
98
| try
|
99
| {
|
100
| // загружаем изображение
|
101
|
|
102
| using (System.IO.FileStream fs = new System.IO.FileStream(ofd.FileName, FileMode.Open))
|
103
| {
|
104
| imageData = new byte[fs.Length];
|
105
| fs.Read(imageData, 0, imageData.Length);
|
106
| }
|
107
| System.IO.MemoryStream ms = new System.IO.MemoryStream(imageData);
|
108
| Image img = System.Drawing.Image.FromStream(ms);
|
109
| pictureBox1.Image = img;
|
110
| FileName = ofd.FileName;
|
111
|
|
112
| }
|
113
| catch // в случае ошибки выводим MessageBox
|
114
| {
|
115
| MessageBox.Show("Невозможно открыть выбранный файл", "Ошибка",
|
116
| MessageBoxButtons.OK, MessageBoxIcon.Error);
|
117
| }
|
118
| }
|
119
| }
|
120
|
|
121
| private void add_Click(object sender, EventArgs e)
|
122
| {
|
123
| SqlCommand comend = new SqlCommand("UPDATE [employee] " +
|
124
| "SET name = @name, surname = @surname, patronymic = @patronymic, specialty = @specialty, login = @login, password = @password, dostoop = @dostoop, image = @image " +
|
125
| "WHERE id = @id; ", sqlConn);//перемеена для хранение запроса
|
126
| comend.Parameters.AddWithValue("@id", id);
|
127
| comend.Parameters.AddWithValue("@name", name.Text);
|
128
| comend.Parameters.AddWithValue("@surname", surname.Text);
|
129
| comend.Parameters.AddWithValue("@patronymic", patronymic.Text);
|
130
| comend.Parameters.AddWithValue("@specialty", specialty.Text);
|
131
| comend.Parameters.AddWithValue("@login", login.Text);
|
132
| comend.Parameters.AddWithValue("@password", password.Text);
|
133
| comend.Parameters.AddWithValue("@dostoop", Convert.ToInt32(dostoop.Text));
|
134
| comend.Parameters.AddWithValue("@image", (byte[])imageData);
|
135
|
|
136
|
|
137
|
|
138
| try
|
139
| {
|
140
| SqlDataReader sqlRead = comend.ExecuteReader();//создаем запрос
|
141
| sqlRead.Close();
|
142
|
|
143
| }
|
144
| catch (Exception ex)//обработка исключений
|
145
| {
|
146
| MessageBox.Show(ex.Message.ToString(), ex.Source.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
|
147
| }
|
148
|
|
149
|
|
150
| this.Hide();
|
151
| }
|
152
| }
|
153
| }
|