Bd.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Npgsql;
  11. using System.Windows.Forms.DataVisualization.Charting;
  12. namespace WindowsFormsApp4
  13. {
  14. public partial class Form1 : Form
  15. {
  16. private string connectionString = "Host=80.250.189.52;Port=5432;Database=dbcourse;Username=druzhinin_ia;Password=q$J37q6#6f%";
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. private void Form1_Load(object sender, EventArgs e)
  22. {
  23. }
  24. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  25. {
  26. }
  27. private void button1_Click(object sender, EventArgs e)
  28. {
  29. NpgsqlConnection connection = null;
  30. connection = new NpgsqlConnection(connectionString);
  31. connection.Open();
  32. string query = "SELECT * FROM \"auto\"";
  33. NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(query, connection);
  34. DataTable dataTable = new DataTable();
  35. adapter.Fill(dataTable);
  36. dataGridView1.DataSource = dataTable;
  37. }
  38. private void button5_Click(object sender, EventArgs e)
  39. {
  40. NpgsqlConnection connection = null;
  41. connection = new NpgsqlConnection(connectionString);
  42. connection.Open();
  43. string query = "SELECT * FROM \"sell_auto\"";
  44. NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(query, connection);
  45. DataTable dataTable = new DataTable();
  46. adapter.Fill(dataTable);
  47. dataGridView1.DataSource = dataTable;
  48. }
  49. private void button4_Click(object sender, EventArgs e)
  50. {
  51. NpgsqlConnection connection = null;
  52. connection = new NpgsqlConnection(connectionString);
  53. connection.Open();
  54. string query = "SELECT * FROM \"users\"";
  55. NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(query, connection);
  56. DataTable dataTable = new DataTable();
  57. adapter.Fill(dataTable);
  58. dataGridView1.DataSource = dataTable;
  59. }
  60. private void chart1_Click(object sender, EventArgs e)
  61. {
  62. }
  63. private void button2_Click(object sender, EventArgs e)
  64. {
  65. Series series = new Series("Автомобили по брендам");
  66. series.ChartType = SeriesChartType.Column;
  67. series.Color = Color.DodgerBlue;
  68. series.BorderColor = Color.DarkBlue;
  69. series.BorderWidth = 2;
  70. series.IsValueShownAsLabel = true;
  71. series.LabelForeColor = Color.White;
  72. series.Font = new Font("Arial", 9, FontStyle.Bold);
  73. using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
  74. {
  75. connection.Open();
  76. string query = @"
  77. SELECT
  78. b.name as brand_name,
  79. COUNT(*) as car_count
  80. FROM auto a
  81. JOIN brand b ON a.brand_id = b.id
  82. GROUP BY b.id, b.name
  83. ORDER BY car_count DESC";
  84. using (NpgsqlCommand cmd = new NpgsqlCommand(query, connection))
  85. using (NpgsqlDataReader reader = cmd.ExecuteReader())
  86. {
  87. while (reader.Read())
  88. {
  89. string brandName = reader.GetString(0);
  90. int carCount = reader.GetInt32(1);
  91. DataPoint point = new DataPoint();
  92. point.SetValueXY(brandName, carCount);
  93. point.Label = carCount.ToString();
  94. series.Points.Add(point);
  95. }
  96. }
  97. }
  98. chart1.Series.Add(series);
  99. chart1.Titles.Add("Распределение автомобилей по брендам");
  100. chart1.Titles[0].Font = new Font("Arial", 14, FontStyle.Bold);
  101. chart1.Titles[0].ForeColor = Color.DarkBlue;
  102. chart1.Update();
  103. }
  104. private void button6_Click(object sender, EventArgs e)
  105. {
  106. var row = dataGridView1.Rows[dataGridView1.Rows.Count - 2];
  107. string sql = $@"
  108. INSERT INTO auto
  109. (
  110. ID, model_id, year_release, VIN_num, color,
  111. retail_price, purchase_price, provider_id
  112. )
  113. VALUES
  114. (
  115. {row.Cells["brand_id"].Value ?? 1},
  116. {row.Cells["model_id"].Value ?? 1},
  117. '{(row.Cells["year_release"].Value ?? DateTime.Today):yyyy-MM-dd}',
  118. '{row.Cells["VIN_num"].Value ?? "NEWVIN"}',
  119. '{row.Cells["color"].Value ?? "Черный"}',
  120. {row.Cells["retail_price"].Value ?? 1000000},
  121. {row.Cells["purchase_price"].Value ?? 800000},
  122. {row.Cells["provider_id"].Value ?? 1}
  123. )";
  124. using (var conn = new NpgsqlConnection(connectionString))
  125. {
  126. conn.Open();
  127. new NpgsqlCommand(sql, conn).ExecuteNonQuery();
  128. }
  129. MessageBox.Show("Добавлено!");
  130. button1_Click(sender, e);
  131. }
  132. private void button3_Click(object sender, EventArgs e)
  133. {
  134. if (dataGridView1.CurrentRow == null || dataGridView1.CurrentRow.IsNewRow)
  135. {
  136. MessageBox.Show("Выберите автомобиль для удаления!");
  137. return;
  138. }
  139. int autoId = Convert.ToInt32(dataGridView1.CurrentRow.Cells["num_auto"].Value);
  140. DialogResult result = MessageBox.Show(
  141. $"Вы действительно хотите удалить автомобиль с ID {autoId}?",
  142. "Подтверждение удаления",
  143. MessageBoxButtons.YesNo,
  144. MessageBoxIcon.Question);
  145. if (result == DialogResult.Yes)
  146. {
  147. string sql = $"DELETE FROM auto WHERE num_auto = {autoId}";
  148. using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
  149. {
  150. conn.Open();
  151. int rowsAffected = new NpgsqlCommand(sql, conn).ExecuteNonQuery();
  152. if (rowsAffected > 0)
  153. {
  154. MessageBox.Show($"Автомобиль с ID {autoId} успешно удален!");
  155. button1_Click(sender, e);
  156. }
  157. else
  158. {
  159. MessageBox.Show("Автомобиль не найден!");
  160. }
  161. }
  162. }
  163. }
  164. private void button7_Click(object sender, EventArgs e)
  165. {
  166. }
  167. private void button7_Click_1(object sender, EventArgs e)
  168. {
  169. var row = dataGridView1.Rows[dataGridView1.Rows.Count - 2];
  170. string sql = $@"
  171. INSERT INTO sell_auto
  172. (
  173. staff_id, client_id, date_sell, num_auto, type_pay
  174. )
  175. VALUES
  176. (
  177. {row.Cells["staff_id"].Value ?? 1},
  178. {row.Cells["client_id"].Value ?? 1},
  179. '{(row.Cells["date_sell"].Value ?? DateTime.Today):yyyy-MM-dd}',
  180. {row.Cells["num_auto"].Value ?? 1},
  181. '{row.Cells["type_pay"].Value ?? "Наличные"}'
  182. )";
  183. using (var conn = new NpgsqlConnection(connectionString))
  184. {
  185. conn.Open();
  186. new NpgsqlCommand(sql, conn).ExecuteNonQuery();
  187. }
  188. MessageBox.Show("Продажа добавлена в таблицу sell_auto!");
  189. button5_Click(sender, e);
  190. }
  191. private void button7_Click_2(object sender, EventArgs e)
  192. {
  193. if (dataGridView1.CurrentRow == null || dataGridView1.CurrentRow.IsNewRow)
  194. {
  195. MessageBox.Show("Выберите автомобиль для изменения!");
  196. return;
  197. }
  198. int autoId = Convert.ToInt32(dataGridView1.CurrentRow.Cells["num_auto"].Value);
  199. DataGridViewRow row = dataGridView1.CurrentRow;
  200. string sql = $@"
  201. UPDATE auto
  202. SET
  203. brand_id = {row.Cells["brand_id"].Value ?? 1},
  204. model_id = {row.Cells["model_id"].Value ?? 1},
  205. year_release = '{(row.Cells["year_release"].Value ?? DateTime.Today):yyyy-MM-dd}',
  206. VIN_num = '{row.Cells["VIN_num"].Value ?? ""}',
  207. color = '{row.Cells["color"].Value ?? ""}',
  208. retail_price = {row.Cells["retail_price"].Value ?? 0},
  209. purchase_price = {row.Cells["purchase_price"].Value ?? 0},
  210. provider_id = {row.Cells["provider_id"].Value ?? 1},
  211. WHERE num_auto = {autoId}";
  212. using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
  213. {
  214. conn.Open();
  215. int rowsAffected = new NpgsqlCommand(sql, conn).ExecuteNonQuery();
  216. if (rowsAffected > 0)
  217. {
  218. MessageBox.Show($"Данные автомобиля с ID {autoId} обновлены!");
  219. button1_Click(sender, e);
  220. }
  221. else
  222. {
  223. MessageBox.Show("Автомобиль не найден!");
  224. }
  225. }
  226. }
  227. }
  228. }