Advertisement
Xsufu

Работа с SQL #1

Nov 3rd, 2020
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  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.Configuration;
  11. using System.Data.SqlClient;
  12.  
  13. namespace DataBase {
  14.     public partial class Form1 : Form {
  15.         public Form1() {
  16.             InitializeComponent();
  17.             listBoxId.Items.Add("Id");
  18.             listBoxMaterial.Items.Add("Материал изготовления");
  19.             listBoxYear.Items.Add("Год строительства");
  20.             listBoxCount.Items.Add("Количество этажей");
  21.             string ConnectionString = @"Data Source=.\SQLEXPRESS; Initial Catalog = Houses; Integrated Security = True";
  22.             string SQLExpression = "SELECT * FROM Houses";
  23.             using (SqlConnection connection = new SqlConnection(ConnectionString)) {
  24.                 connection.Open();
  25.                 SqlCommand command = new SqlCommand(SQLExpression, connection);
  26.                 SqlDataReader reader = command.ExecuteReader();
  27.  
  28.                 if (reader.HasRows) { //Если есть данные
  29.                     while (reader.Read()) { //Ввод данных в боксы
  30.                         listBoxId.Items.Add(reader.GetValue(0));
  31.                         listBoxMaterial.Items.Add(reader.GetValue(1));
  32.                         listBoxYear.Items.Add(reader.GetValue(2));
  33.                         listBoxCount.Items.Add(reader.GetValue(3));
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.  
  39.         private void button1_Click(object sender, EventArgs e) {
  40.             string ConnectionString = @"Data Source=.\SQLEXPRESS; Initial Catalog = Houses; Integrated Security = True";
  41.             string SQLExpression = "SELECT * FROM Houses WHERE ([Количество этажей] > 9 AND Материал = 'Кирпич')";
  42.             listBox1.Items.Clear();
  43.             using (SqlConnection connection = new SqlConnection(ConnectionString)) {
  44.                 connection.Open();
  45.                 SqlCommand command = new SqlCommand(SQLExpression, connection);
  46.                 SqlDataReader reader = command.ExecuteReader();
  47.  
  48.                 if (reader.HasRows) { //Если есть данные
  49.                     while (reader.Read()) { //Ввод данных в боксы
  50.                         listBox1.Items.Add($"Id: {reader.GetValue(0)}, {reader.GetValue(1)}, {reader.GetValue(2)}г., этажей: {reader.GetValue(3)}");
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.  
  56.         private void button2_Click(object sender, EventArgs e) {
  57.             string ConnectionString = @"Data Source=.\SQLEXPRESS; Initial Catalog = Houses; Integrated Security = True";
  58.             string SQLExpression = "SELECT * FROM Houses WHERE ([Год строительства] < 1992 AND Материал = 'Панель')";
  59.             listBox1.Items.Clear();
  60.             using (SqlConnection connection = new SqlConnection(ConnectionString)) {
  61.                 connection.Open();
  62.                 SqlCommand command = new SqlCommand(SQLExpression, connection);
  63.                 SqlDataReader reader = command.ExecuteReader();
  64.  
  65.                 if (reader.HasRows) { //Если есть данные
  66.                     while (reader.Read()) { //Ввод данных в боксы
  67.                         listBox1.Items.Add($"Id: {reader.GetValue(0)}, {reader.GetValue(1)}, {reader.GetValue(2)}г., этажей: {reader.GetValue(3)}");
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement