Browse Source

добавление кода на форму1 и подключение кнопок

Kuzmenco 1 year ago
parent
commit
fa1b69162d
3 changed files with 73 additions and 2 deletions
  1. 15 0
      Form1.Designer.cs
  2. 52 2
      Form1.cs
  3. 6 0
      Form1.resx

+ 15 - 0
Form1.Designer.cs

@@ -44,6 +44,7 @@ namespace Lab1_
             this.button1 = new System.Windows.Forms.Button();
             this.button2 = new System.Windows.Forms.Button();
             this.button3 = new System.Windows.Forms.Button();
+            this.name = new System.Windows.Forms.TextBox();
             ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.aero03DataSet)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.tripBindingSource)).BeginInit();
@@ -132,6 +133,7 @@ namespace Lab1_
             this.button1.TabIndex = 1;
             this.button1.Text = "Авиакомпании";
             this.button1.UseVisualStyleBackColor = true;
+            this.button1.Click += new System.EventHandler(this.button1_Click);
             // 
             // button2
             // 
@@ -141,6 +143,7 @@ namespace Lab1_
             this.button2.TabIndex = 2;
             this.button2.Text = "Добавить";
             this.button2.UseVisualStyleBackColor = true;
+            this.button2.Click += new System.EventHandler(this.button2_Click);
             // 
             // button3
             // 
@@ -150,12 +153,22 @@ namespace Lab1_
             this.button3.TabIndex = 3;
             this.button3.Text = "Удалить изменения";
             this.button3.UseVisualStyleBackColor = true;
+            this.button3.Click += new System.EventHandler(this.button3_Click);
+            // 
+            // name
+            // 
+            this.name.Location = new System.Drawing.Point(466, 565);
+            this.name.MaxLength = 10;
+            this.name.Name = "name";
+            this.name.Size = new System.Drawing.Size(100, 20);
+            this.name.TabIndex = 4;
             // 
             // Form1
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(786, 606);
+            this.Controls.Add(this.name);
             this.Controls.Add(this.button3);
             this.Controls.Add(this.button2);
             this.Controls.Add(this.button1);
@@ -167,6 +180,7 @@ namespace Lab1_
             ((System.ComponentModel.ISupportInitialize)(this.aero03DataSet)).EndInit();
             ((System.ComponentModel.ISupportInitialize)(this.tripBindingSource)).EndInit();
             this.ResumeLayout(false);
+            this.PerformLayout();
 
         }
 
@@ -186,6 +200,7 @@ namespace Lab1_
         private System.Windows.Forms.Button button1;
         private System.Windows.Forms.Button button2;
         private System.Windows.Forms.Button button3;
+        private System.Windows.Forms.TextBox name;
     }
 }
 

+ 52 - 2
Form1.cs

@@ -7,21 +7,71 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
+using System.Data.SqlClient;
+
+namespace Lab1_{
+
 
-namespace Lab1_
-{
     public partial class Form1 : Form
     {
+        SqlConnection connection;
+        SqlCommand command;
+        SqlDataAdapter adapter;
+        DataTable table;
+
         public Form1()
         {
+
             InitializeComponent();
+            dataGridView1.AutoGenerateColumns = true;
+            // УКАЖИТЕ СВОЙ АДРЕС СЕРВЕРА ИЗ СРЕДЫ SSMS!
+            connection = new SqlConnection("Server=DESKTOP‐PD8FNND\\SQLEXPRESS;Database=aero03;Trusted_Connection=True;");
+            command = new SqlCommand();
+            command.Connection = connection;
+            command.CommandType = CommandType.Text;
+            adapter = new SqlDataAdapter(command);
+            table = new DataTable();
+            dataGridView1.DataSource = table;
         }
 
         private void Form1_Load(object sender, EventArgs e)
         {
             // TODO: данная строка кода позволяет загрузить данные в таблицу "aero03DataSet.Trip". При необходимости она может быть перемещена или удалена.
             this.tripTableAdapter.Fill(this.aero03DataSet.Trip);
+        }
+            private void ShowTable(string text)
+            {
+                dataGridView1.Columns.Clear();
+                dataGridView1.DataSource = null;
+                command.CommandText = text;
+                table.Clear();
+                adapter.Fill(table);
+                dataGridView1.DataSource = table;
+
+            }
+        
 
+        private void button1_Click(object sender, EventArgs e)
+        {
+          ShowTable("SELECT * FROM Company");
+        }
+
+        private void button2_Click(object sender, EventArgs e)
+        {
+                connection.Open();
+                command.CommandText = "INSERT INTO Company VALUES (\'" + name.Text + "\');";
+                command.ExecuteReader();
+                connection.Close();
+                ShowTable("SELECT * FROM Company");
+        }
+
+        private void button3_Click(object sender, EventArgs e)
+        {
+                connection.Open();
+                command.CommandText = "DELETE FROM Company WHERE ID_comp>= 6";
+                command.ExecuteReader();
+                connection.Close();
+                ShowTable("SELECT * FROM Company");
         }
     }
 }

+ 6 - 0
Form1.resx

@@ -123,6 +123,12 @@
   <metadata name="aero03DataSet.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>17, 17</value>
   </metadata>
+  <metadata name="tripBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>149, 17</value>
+  </metadata>
+  <metadata name="aero03DataSet.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>17, 17</value>
+  </metadata>
   <metadata name="tripTableAdapter.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>298, 17</value>
   </metadata>