Advertisement
Guest User

Untitled

a guest
Nov 5th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.49 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.IO; // для сохранения файла на диск
  11.  
  12. using Npgsql;
  13.  
  14. using System.Threading;
  15. using System.Collections;
  16.  
  17.  
  18. public struct things
  19. {
  20.     public int id, cost;
  21.     public int[] cat, tp, nm;
  22.     public string category, type, name;
  23. }
  24.  
  25. namespace DGV
  26. {
  27.     public partial class Form1 : Form
  28.     {
  29.  
  30.         private static string connectionString = "Server=localhost;Port=5432;User=postgres;Password=12345;Database=data";
  31.         private static string insertCategoryString = "INSERT INTO categories(id, name, id_parent) VALUES (@id, @name, @id_parent)";
  32.         private static string insertProductString = "INSERT INTO products(id, name, cost, image, description, id_category) VALUES (@id, @name, @cost, @image, @description, @id_category)";
  33.         private static string selectString = "SELECT p.id, p.name, p.cost, p.image, p.description, c.name FROM products p LEFT JOIN categories c on p.id_category = c.id ORDER BY p.id";
  34.         private static string deleteString = "DELETE FROM products WHERE id = ";
  35.  
  36.         private int currentPage = 0;
  37.  
  38.         private string currentRow;
  39.         List<Row> rowList = new List<Row>();
  40.  
  41.         NpgsqlConnection mainConnection = new NpgsqlConnection(connectionString);
  42.        
  43.  
  44.         public Form1()
  45.         {
  46.             InitializeComponent();
  47.             mainConnection.Open();
  48.             dataGridView1.SuspendLayout();
  49.         }
  50.  
  51.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  52.         {
  53.         }
  54.        
  55.  
  56.         private void button1_Click(object sender, EventArgs e)
  57.         {
  58.             createProducts();
  59.         }
  60.  
  61.  
  62.         private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
  63.         {
  64.          //   DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  65.             currentRow = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
  66.             textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
  67.             textBox2.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
  68.             textBox3.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
  69.             textBox4.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
  70.         }
  71.  
  72.         private void button2_Click(object sender, EventArgs e) //редактирование
  73.         {
  74.             if (dataGridView1.SelectedRows.Count == 1)
  75.             {
  76.                 try
  77.                 {
  78.                     string str = "UPDATE products SET " + "name= '" + textBox3.Text + "', cost= " + textBox4.Text + ", description= '" + textBox2.Text + "', id_category= " + textBox1.Text + " WHERE id = " + currentRow;
  79.                     Console.WriteLine(str);
  80.                     NpgsqlCommand command = new NpgsqlCommand(str, mainConnection);
  81.                     command.ExecuteReader();
  82.  
  83.                     int n = dataGridView1.SelectedRows[0].Index;
  84.                     dataGridView1.Rows[n].Cells[1].Value = textBox1.Text;
  85.                     dataGridView1.Rows[n].Cells[2].Value = textBox2.Text;
  86.                     dataGridView1.Rows[n].Cells[3].Value = textBox3.Text;
  87.                     dataGridView1.Rows[n].Cells[4].Value = textBox4.Text;
  88.                 }
  89.                 catch (Exception exc)
  90.                 {
  91.                     MessageBox.Show("Data is incorrect");
  92.                 }
  93.             }
  94.             else
  95.             {
  96.                 MessageBox.Show("Выберите строку для редактирования.", "Ошибка.");
  97.             }
  98.         }
  99.  
  100.         private void button3_Click(object sender, EventArgs e) //удалить выбранную строку
  101.         {
  102.             if (dataGridView1.SelectedRows.Count > 0)
  103.             {
  104.                 Console.WriteLine(deleteString + currentRow);
  105.                 NpgsqlCommand command = new NpgsqlCommand(deleteString + currentRow, mainConnection);
  106.                 command.ExecuteReader();
  107.                 dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
  108.             }
  109.             else
  110.             {
  111.                 MessageBox.Show("Выберите строку для удаления.", "Ошибка.");
  112.             }
  113.         }
  114.  
  115.         private void button4_Click(object sender, EventArgs e) //очистить таблицу
  116.         {
  117.             if (dataGridView1.Rows.Count > 0)
  118.             {
  119.                 dataGridView1.Rows.Clear();
  120.             }
  121.             else
  122.             {
  123.                 MessageBox.Show("Таблица пустая.", "Ошибка.");
  124.             }
  125.         }
  126.  
  127.         private void button5_Click(object sender, EventArgs e)
  128.         {
  129.             addRow();
  130.         }
  131.  
  132.         private void createCategories()
  133.         {
  134.             NpgsqlConnection connection = new NpgsqlConnection(connectionString);
  135.             connection.Open();
  136.             int mainCategoriesNumber = 40;
  137.             int subcategoriesNumber = 5;
  138.             int id = 0;
  139.             for (int i = 1; i < mainCategoriesNumber; i++) {
  140.                 NpgsqlCommand command = new NpgsqlCommand(insertCategoryString, connection);
  141.                 id++;
  142.  
  143.                 var idParameter = command.CreateParameter();
  144.                 idParameter.ParameterName = "id";
  145.                 idParameter.Value = id;
  146.                 command.Parameters.Add(idParameter);
  147.  
  148.                 var nameParameter = command.CreateParameter();
  149.                 nameParameter.ParameterName = "name";
  150.                 nameParameter.Value = "Категория " + id;
  151.                 command.Parameters.Add(nameParameter);
  152.  
  153.                 var idParParameter = command.CreateParameter();
  154.                 idParParameter.ParameterName = "id_parent";
  155.                 idParParameter.Value = 0;
  156.                 command.Parameters.Add(idParParameter);
  157.  
  158.                 NpgsqlDataReader dr = command.ExecuteReader();
  159.             }
  160.  
  161.             for (int i = 1; i < mainCategoriesNumber; i++)
  162.             {
  163.                 int parent_id = i;
  164.                 for (int j = 1; j < subcategoriesNumber; j++)
  165.                 {
  166.                     NpgsqlCommand command = new NpgsqlCommand(insertCategoryString, connection);
  167.                     id++;
  168.                     var idParameter = command.CreateParameter();
  169.                     idParameter.ParameterName = "id";
  170.                     idParameter.Value = id;
  171.                     command.Parameters.Add(idParameter);
  172.  
  173.                     var nameParameter = command.CreateParameter();
  174.                     nameParameter.ParameterName = "name";
  175.                     nameParameter.Value = "Категория " + parent_id + "." + j;
  176.                     command.Parameters.Add(nameParameter);
  177.  
  178.                     var idParParameter = command.CreateParameter();
  179.                     idParParameter.ParameterName = "id_parent";
  180.                     idParParameter.Value = parent_id;
  181.                     command.Parameters.Add(idParParameter);
  182.  
  183.                     NpgsqlDataReader dr = command.ExecuteReader();
  184.                 }
  185.             }
  186.            
  187.             connection.Close();
  188.         }
  189.  
  190.         private void createProducts()
  191.         {
  192.             NpgsqlConnection connection = new NpgsqlConnection(connectionString);
  193.             connection.Open();
  194.             int productsNumber = 50000;
  195.             Random rnd = new Random();
  196.             int id = 0;
  197.             for (int i = 1; i < productsNumber; i++)
  198.             {
  199.                 NpgsqlCommand command = new NpgsqlCommand(insertProductString, connection);
  200.                 id++;
  201.  
  202.                 var idParameter = command.CreateParameter();
  203.                 idParameter.ParameterName = "id";
  204.                 idParameter.Value = id;
  205.                 command.Parameters.Add(idParameter);
  206.  
  207.                 var nameParameter = command.CreateParameter();
  208.                 nameParameter.ParameterName = "name";
  209.                 nameParameter.Value = "Имя " + id;
  210.                 command.Parameters.Add(nameParameter);
  211.  
  212.                 var costParameter = command.CreateParameter();
  213.                 costParameter.ParameterName = "cost";
  214.                 costParameter.Value = rnd.NextDouble()*200000;
  215.                 command.Parameters.Add(costParameter);
  216.  
  217.                 var ImageParameter = command.CreateParameter();
  218.                 ImageParameter.ParameterName = "image";
  219.                 ImageParameter.Value = "C:\\LAB1\\Folder\\" + id +".jpg";
  220.                 command.Parameters.Add(ImageParameter);
  221.  
  222.                 var descriptionParameter = command.CreateParameter();
  223.                 descriptionParameter.ParameterName = "description";
  224.                 descriptionParameter.Value = "Описание " + i;
  225.                 command.Parameters.Add(descriptionParameter);
  226.  
  227.                 var idCatParameter = command.CreateParameter();
  228.                 idCatParameter.ParameterName = "id_category";
  229.                 idCatParameter.Value = rnd.Next (1, 195);
  230.                 command.Parameters.Add(idCatParameter);
  231.  
  232.                 NpgsqlDataReader dr = command.ExecuteReader();
  233.             }
  234.             Console.WriteLine("Data Inserted");
  235.             connection.Close();
  236.         }
  237.  
  238.         private void addRow() {
  239.             try
  240.             {
  241.                 NpgsqlCommand maxCommand = new NpgsqlCommand("SELECT MAX (id) as max_id FROM products", mainConnection);
  242.                 NpgsqlDataReader maxDr = maxCommand.ExecuteReader();
  243.                 maxDr.Read();
  244.                 int id = Convert.ToInt32(maxDr[0].ToString());
  245.                 id++;
  246.  
  247.                 maxDr.Close();
  248.                 string str;
  249.                 str = "INSERT INTO products(id, name, cost, image, description, id_category)" +
  250.                                                             " VALUES (" + id.ToString() + ", '" + textBox3.Text + "', " + textBox4.Text + ", '" + textBox5.Text + "', '" + textBox2.Text + "', " + textBox1.Text + " )";
  251.                 Console.WriteLine(str);
  252.                 NpgsqlCommand command = new NpgsqlCommand(str, mainConnection);
  253.                 command.ExecuteReader();
  254.  
  255.                 int n = dataGridView1.Rows.Add();
  256.                 dataGridView1.Rows[n].Cells[0].Value = id;
  257.                 dataGridView1.Rows[n].Cells[1].Value = textBox1.Text;
  258.                 dataGridView1.Rows[n].Cells[2].Value = textBox2.Text;
  259.                 dataGridView1.Rows[n].Cells[3].Value = textBox3.Text;
  260.                 dataGridView1.Rows[n].Cells[4].Value = textBox4.Text;
  261.             }
  262.             catch (Exception e) {
  263.                 MessageBox.Show("Data is incorrect");
  264.             }
  265.            
  266.         }
  267.  
  268.         private void select() {
  269.             try
  270.             {
  271.                 NpgsqlConnection connection = new NpgsqlConnection(connectionString);
  272.                 connection.Open();
  273.                 NpgsqlCommand command = new NpgsqlCommand(selectString, connection);
  274.  
  275.                 NpgsqlDataReader dr = command.ExecuteReader();
  276.                 Row row;
  277.                 rowList = new List<Row>();
  278.                 while (dr.Read())
  279.                 {
  280.                     row = new Row(dr[0].ToString(), dr[5].ToString(), dr[4].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString());
  281.                     rowList.Add(row);
  282.                     //dataGridView1.Rows.Add(dr[0], dr[5], dr[4], dr[1], dr[2], Image.FromFile(dr[3].ToString()));
  283.                 }
  284.                 select(0);
  285.                 Console.WriteLine(rowList.Count);
  286.                 dr.Close();
  287.                 connection.Close();
  288.             }
  289.             catch (Exception exception) {
  290.                 MessageBox.Show("Error while loading data");
  291.                 //Можно заблокировать все кнопки
  292.                 //textBox1.Enabled = false;
  293.             }
  294.            
  295.         }
  296.  
  297.         private void select(int i)
  298.         {
  299.             if (dataGridView1.Rows.Count > 0)
  300.             {
  301.                 dataGridView1.Rows.Clear();
  302.             }
  303.             Row row;
  304.             for (int p = i*500; p < i*500 + 500; p++) {
  305.                 row = rowList.ElementAt(p);
  306.                 dataGridView1.Rows.Add(row.id, row.category, row.description, row.name, row.cost, Image.FromFile(row.image));
  307.             }
  308.              
  309.         }
  310.  
  311.         private void button6_Click(object sender, EventArgs e)
  312.         {
  313.             select();
  314.         }
  315.  
  316.         public class Row {
  317.             public Row(string id, string category, string description, string name, string cost, string image) {
  318.                 this.id = id;
  319.                 this.category = category;
  320.                 this.description = description;
  321.                 this.name = name;
  322.                 this.cost = cost;
  323.                 this.image = image;
  324.             }
  325.             public string id; //dr[0]
  326.             public string category; //dr[1]
  327.             public string description; //dr[2]
  328.             public string name; //dr[3]
  329.             public string cost; //dr[4]
  330.             public string image; //dr[5]
  331.         }
  332.  
  333.         private void button8_Click(object sender, EventArgs e) //next page
  334.         {
  335.             currentPage++;
  336.             select(currentPage);
  337.         }
  338.  
  339.         private void button7_Click(object sender, EventArgs e) //previous page
  340.         {
  341.             currentPage--;
  342.             select(currentPage);
  343.         }
  344.     }
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement