Form1.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 System.Data.SqlClient;
  11. namespace Lab1_{
  12. public partial class Form1 : Form
  13. {
  14. SqlConnection connection;
  15. SqlCommand command;
  16. SqlDataAdapter adapter;
  17. DataTable table;
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. dataGridView1.AutoGenerateColumns = true;
  22. // УКАЖИТЕ СВОЙ АДРЕС СЕРВЕРА ИЗ СРЕДЫ SSMS!
  23. connection = new SqlConnection("Server=DESKTOP‐PD8FNND\\SQLEXPRESS;Database=aero03;Trusted_Connection=True;");
  24. command = new SqlCommand();
  25. command.Connection = connection;
  26. command.CommandType = CommandType.Text;
  27. adapter = new SqlDataAdapter(command);
  28. table = new DataTable();
  29. dataGridView1.DataSource = table;
  30. }
  31. private void Form1_Load(object sender, EventArgs e)
  32. {
  33. // TODO: данная строка кода позволяет загрузить данные в таблицу "aero03DataSet.Trip". При необходимости она может быть перемещена или удалена.
  34. this.tripTableAdapter.Fill(this.aero03DataSet.Trip);
  35. }
  36. private void ShowTable(string text)
  37. {
  38. dataGridView1.Columns.Clear();
  39. dataGridView1.DataSource = null;
  40. command.CommandText = text;
  41. table.Clear();
  42. adapter.Fill(table);
  43. dataGridView1.DataSource = table;
  44. }
  45. private void button1_Click(object sender, EventArgs e)
  46. {
  47. ShowTable("SELECT * FROM Company");
  48. }
  49. private void button2_Click(object sender, EventArgs e)
  50. {
  51. connection.Open();
  52. command.CommandText = "INSERT INTO Company VALUES (\'" + name.Text + "\');";
  53. command.ExecuteReader();
  54. connection.Close();
  55. ShowTable("SELECT * FROM Company");
  56. }
  57. private void button3_Click(object sender, EventArgs e)
  58. {
  59. connection.Open();
  60. command.CommandText = "DELETE FROM Company WHERE ID_comp>= 6";
  61. command.ExecuteReader();
  62. connection.Close();
  63. ShowTable("SELECT * FROM Company");
  64. }
  65. }
  66. }