Vla_DOS

Untitled

Jan 29th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.30 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Windows.Forms;
  4. using System.Data.SqlClient;
  5. using System.Configuration;
  6.  
  7. namespace WindowsFormsAppDB
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         SqlConnection sqlConnection = null;
  12.         //static SqlCommand commandCatalogInMusicStore;
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private void Form1_Load(object sender, EventArgs e)
  19.         {
  20.  
  21.             this.toolsTableAdapter.Fill(this.agriculturalMachineryDataSet.Tools);
  22.  
  23.             this.orderTableAdapter.Fill(this.agriculturalMachineryDataSet.Order);
  24.  
  25.             sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["AgriculturalMachinery"].ConnectionString);
  26.  
  27.             sqlConnection.Open();
  28.  
  29.             //if(sqlConnection.State == ConnectionState.Open)
  30.             //    MessageBox.Show("Connected good!");
  31.         }
  32.  
  33.         string SELECTIDENTITY(SqlCommand command, string IdVal) =>
  34.             command.CommandText = $"SELECT IDENT_CURRENT('{IdVal}')";
  35.  
  36.  
  37.         //private void button3_Click(object sender, EventArgs e)
  38.         //{
  39.         //    if(tB_IdCustomer.Text != string.Empty || tb_NameCustomer.Text != string.Empty || tB_PhoneNumber.Text != string.Empty || tb_Address.Text != string.Empty || tb_CurrentAccount.Text != string.Empty)
  40.         //    {
  41.  
  42.         //        //Додаємо Customers
  43.         //        SqlCommand commandCustomers = new SqlCommand(
  44.         //            "INSERT INTO [Customers] (IdCustomer, NameCustomer, PhoneNumber, Address, CurrentAccount) " +
  45.         //            "VALUES (@IdCustomer, @NameCustomer, @PhoneNumber, @Address, @CurrentAccount)", sqlConnection);
  46.  
  47.         //        commandCustomers.Parameters.AddWithValue("IdCustomer", tB_IdCustomer.Text);
  48.         //        commandCustomers.Parameters.AddWithValue("NameCustomer", tb_NameCustomer.Text);
  49.         //        commandCustomers.Parameters.AddWithValue("PhoneNumber", tB_PhoneNumber.Text);
  50.         //        commandCustomers.Parameters.AddWithValue("Address", tb_Address.Text);
  51.         //        commandCustomers.Parameters.AddWithValue("CurrentAccount", tb_CurrentAccount.Text);
  52.  
  53.         //        MessageBox.Show(commandCustomers.ExecuteNonQuery().ToString());
  54.         //        sqlConnection.Close();
  55.         //    }
  56.         //    else
  57.         //        MessageBox.Show("Будь ласка, введіть дані!");
  58.         //}
  59.  
  60.         //private void button4_Click(object sender, EventArgs e)
  61.         //{
  62.         //    if(tb_IdTool.Text != string.Empty || tb_NameTool.Text != string.Empty || tb_NumberTool.Text != string.Empty)
  63.         //    {
  64.         //        sqlConnection.Open();
  65.  
  66.         //        //Додаємо Tools
  67.         //        SqlCommand commandTools = new SqlCommand(
  68.         //        "INSERT INTO [Tools] (IdTool, NameTool, NumberTool) " +
  69.         //        "VALUES (@IdTool, @NameTool, @NumberTool)", sqlConnection);
  70.  
  71.         //        commandTools.Parameters.AddWithValue("IdTool", tb_IdTool.Text);
  72.         //        commandTools.Parameters.AddWithValue("NameTool", tb_NameTool.Text);
  73.         //        commandTools.Parameters.AddWithValue("NumberTool", tb_NumberTool.Text);
  74.  
  75.  
  76.         //        MessageBox.Show(commandTools.ExecuteNonQuery().ToString());
  77.         //        sqlConnection.Close();
  78.  
  79.         //    }
  80.         //    else
  81.         //        MessageBox.Show("Будь ласка, введіть дані!");
  82.         //}
  83.         private void tb_Insert_Click(object sender, EventArgs e)
  84.         {
  85.             if(tB_Id.Text != string.Empty || tB_CustomerId.Text != string.Empty || tb_ToolId.Text != string.Empty || tb_Number.Text != string.Empty)
  86.             {
  87.                 sqlConnection.Open();
  88.  
  89.                 //Додаємо Order
  90.                 SqlCommand commandOrder = new SqlCommand(
  91.                 "INSERT INTO [Order] (Id, CustomerId, ToolId, Number, Date, Done_NotDone) " +
  92.                 "VALUES (@Id, @CustomerId, @ToolId, @Number, @Date, @Done_NotDone)", sqlConnection);
  93.  
  94.                 commandOrder.Parameters.AddWithValue("Id", tB_Id.Text);
  95.                 commandOrder.Parameters.AddWithValue("CustomerId", tB_CustomerId.Text);
  96.                 commandOrder.Parameters.AddWithValue("ToolId", tb_ToolId.Text);
  97.                 commandOrder.Parameters.AddWithValue("Number", tb_Number.Text);
  98.                 commandOrder.Parameters.AddWithValue("Date", tb_Date.Text);
  99.                 commandOrder.Parameters.AddWithValue("Done_NotDone", chB_Done_NotDone.Checked);
  100.  
  101.                 MessageBox.Show(commandOrder.ExecuteNonQuery().ToString());
  102.                 sqlConnection.Close();
  103.  
  104.             }
  105.             else
  106.                 MessageBox.Show("Будь ласка, введіть дані!");
  107.  
  108.             //SELECTIDENTITY(commandRecordings, "Recordings");
  109.  
  110.             //var c = commandOrder.ExecuteScalar().ToString();
  111.             // MessageBox.Show(c);
  112.             //Done_NotDone(chB_Done_NotDone.ToString());
  113.  
  114.  
  115.  
  116.  
  117.             //SELECTIDENTITY(commandPerformers, "Performers");
  118.  
  119.             //var c1 = commandPerformers.ExecuteScalar().ToString();
  120.  
  121.  
  122.  
  123.  
  124.             //SELECTIDENTITY(commandGenres, "Genres");
  125.  
  126.             //var c2 = commandGenres.ExecuteScalar().ToString();
  127.  
  128.  
  129.             //// Додаємо RecordCompanys
  130.             //SqlCommand commandRecordCompanys = new SqlCommand(
  131.             //    "INSERT INTO [RecordCompanys] (RecordCompany) " +
  132.             //    "VALUES (@RecordCompany)", sqlConnection);
  133.  
  134.             //commandRecordCompanys.Parameters.AddWithValue("RecordCompany", t_B_RecordCompanys.Text);
  135.  
  136.             //commandRecordCompanys.ExecuteNonQuery().ToString();
  137.  
  138.             //SELECTIDENTITY(commandRecordCompanys, "RecordCompanys");
  139.  
  140.             //var c4 = commandRecordCompanys.ExecuteScalar().ToString();
  141.  
  142.  
  143.             ////Додаємо InformationAboutPerformers
  144.             //SqlCommand commandInformationAboutPerformers = new SqlCommand(
  145.             //    "INSERT INTO [InfoAboutPerformers] (FirstName, LastName, DateOfBirth, PlaceOfBirth, Professions, Groups) " +
  146.             //    "VALUES (@FirstName, @LastName, @DateOfBirth, @PlaceOfBirth, @Professions, @Groups)", sqlConnection);
  147.  
  148.             //commandInformationAboutPerformers.Parameters.AddWithValue("FirstName", tb_FirstName.Text);
  149.             //commandInformationAboutPerformers.Parameters.AddWithValue("LastName", tb_LastName.Text);
  150.             //commandInformationAboutPerformers.Parameters.AddWithValue("DateOfBirth", tB_DateOfBirth.Text);
  151.             //commandInformationAboutPerformers.Parameters.AddWithValue("PlaceOfBirth", tb_PlaceOfBirth.Text);
  152.             //commandInformationAboutPerformers.Parameters.AddWithValue("Professions", tb_Professions.Text);
  153.             //commandInformationAboutPerformers.Parameters.AddWithValue("Groups", tb_Groups.Text);
  154.  
  155.             //commandInformationAboutPerformers.ExecuteNonQuery().ToString();
  156.  
  157.             //SELECTIDENTITY(commandInformationAboutPerformers, "InfoAboutPerformers");
  158.  
  159.             //var c3 = commandInformationAboutPerformers.ExecuteScalar().ToString();
  160.  
  161.             //commandCatalogInMusicStore = new SqlCommand("INSERT INTO [CatalogInMusicStore] (IdRecords, IdPerformers, IdGenres, IdRecordCompanys, Price, Quantity, NumberOfSales, IdInformationAboutPerformers) " +
  162.             //    "VALUES (@IdRecords, @IdPerformers, @IdGenres, @IdRecordCompanys, @Price, @Quantity, @NumberOfSales, @IdInformationAboutPerformers)", sqlConnection);
  163.  
  164.  
  165.  
  166.             //commandCatalogInMusicStore.Parameters.AddWithValue("IdRecords", c);
  167.  
  168.             //MessageBox.Show(c);
  169.  
  170.             //commandCatalogInMusicStore.Parameters.AddWithValue("IdPerformers", c1);
  171.  
  172.             //commandCatalogInMusicStore.Parameters.AddWithValue("IdGenres", c2);
  173.  
  174.  
  175.  
  176.  
  177.             //commandCatalogInMusicStore.Parameters.AddWithValue("IdRecordCompanys", c4);
  178.  
  179.  
  180.             //commandCatalogInMusicStore.Parameters.AddWithValue("Price", tB_Id.Text);
  181.             //commandCatalogInMusicStore.Parameters.AddWithValue("Quantity", tB_CustomerId.Text);
  182.             //commandCatalogInMusicStore.Parameters.AddWithValue("NumberOfSales", tb_ToolId.Text);
  183.  
  184.  
  185.             //commandCatalogInMusicStore.Parameters.AddWithValue("IdInformationAboutPerformers", c3);
  186.             //commandCatalogInMusicStore.ExecuteNonQuery().ToString();
  187.         }
  188.  
  189.  
  190.  
  191.  
  192.         private void btn_Album_Click(object sender, EventArgs e)
  193.         {
  194.             if(tb_Album.Text != string.Empty)
  195.             {
  196.                 SqlDataAdapter dataAdapter = new SqlDataAdapter($"select Id as 'Код замовлення', IdTool 'Код інструмента', NameTool as 'Назва інструмента' from [Order], [Tools]where NameTool = N'{tb_Album.Text}'", sqlConnection);
  197.  
  198.                 DataSet dataSet = new DataSet();
  199.  
  200.                 dataAdapter.Fill(dataSet);
  201.                 dataGridView1.DataSource = dataSet.Tables[0];
  202.  
  203.             }
  204.             else
  205.                 MessageBox.Show("Будь ласка, введіть дані!");
  206.  
  207.  
  208.         }
  209.  
  210.  
  211.         private void btn_Perfomers_Click(object sender, EventArgs e)
  212.         {
  213.             if(tb_Perfomers.Text != string.Empty)
  214.             {
  215.                 SqlDataAdapter dataAdapter = new SqlDataAdapter($"SELECT CatalogInMusicStore.Id, Performers.Performer, Genres.Genre, RecordCompanys.RecordCompany,CatalogInMusicStore.Price,  CatalogInMusicStore.Quantity FROM CatalogInMusicStore, Performers, Genres, Recordings, RecordCompanys WHERE CatalogInMusicStore.IdPerformers = Performers.IdPerformer AND CatalogInMusicStore.IdGenres = Genres.IdGenre AND CatalogInMusicStore.IdRecords = Recordings.IdRecord AND CatalogInMusicStore.IdRecordCompanys = RecordCompanys.IdRecordCompany AND Performers.Performer = '{tb_Perfomers.Text}'", sqlConnection);
  216.  
  217.                 DataSet dataSet = new DataSet();
  218.  
  219.                 dataAdapter.Fill(dataSet);
  220.                 dataGridView1.DataSource = dataSet.Tables[0];
  221.  
  222.                 SqlDataAdapter dataAdapter1 = new SqlDataAdapter($"SELECT Recordings.AlbumTitle, Recordings.DateOfSale, Recordings.NumberOfSales FROM CatalogInMusicStore, Recordings, RecordCompanys, Performers WHERE CatalogInMusicStore.IdRecords = Recordings.IdRecord AND CatalogInMusicStore.IdPerformers = Performers.IdPerformer AND CatalogInMusicStore.IdRecordCompanys = RecordCompanys.IdRecordCompany AND Performers.Performer = '{tb_Perfomers.Text}'", sqlConnection);
  223.  
  224.                 DataSet dataSet1 = new DataSet();
  225.  
  226.                 dataAdapter1.Fill(dataSet1);
  227.                 dataGridView2.DataSource = dataSet1.Tables[0];
  228.  
  229.                 SqlDataAdapter dataAdapter2 = new SqlDataAdapter($"SELECT InfoAboutPerformers.FirstName, InfoAboutPerformers.LastName, InfoAboutPerformers.DateOfBirth,  InfoAboutPerformers.PlaceOfBirth, InfoAboutPerformers.Professions, InfoAboutPerformers.Groups FROM CatalogInMusicStore, InfoAboutPerformers, RecordCompanys, Performers WHERE CatalogInMusicStore.IdInformationAboutPerformers = InfoAboutPerformers.IdPerfomersInfo AND CatalogInMusicStore.IdInformationAboutPerformers = RecordCompanys.IdRecordCompany AND Performers.Performer = '{tb_Perfomers.Text}'", sqlConnection);
  230.  
  231.                 DataSet dataSet2 = new DataSet();
  232.  
  233.                 dataAdapter2.Fill(dataSet2);
  234.                 dataGridView3.DataSource = dataSet2.Tables[0];
  235.             }
  236.             else
  237.                 MessageBox.Show("Будь ласка, введіть дані!");
  238.         }
  239.  
  240.         private void btn_RecordCompanys_Click(object sender, EventArgs e)
  241.         {
  242.             if(tb_RecordCompanys.Text != string.Empty)
  243.             {
  244.                 SqlDataAdapter dataAdapter = new SqlDataAdapter($"SELECT Performers.Performer, Genres.Genre, RecordCompanys.RecordCompany, CatalogInMusicStore.Price, CatalogInMusicStore.Quantity FROM CatalogInMusicStore, Performers, Genres, Recordings, RecordCompanys WHERE CatalogInMusicStore.IdPerformers = Performers.IdPerformer AND CatalogInMusicStore.IdGenres = Genres.IdGenre and CatalogInMusicStore.IdRecords = Recordings.IdRecord and CatalogInMusicStore.IdRecordCompanys = RecordCompanys.IdRecordCompany and RecordCompanys.RecordCompany = '{tb_RecordCompanys.Text}'", sqlConnection);
  245.  
  246.                 DataSet dataSet = new DataSet();
  247.  
  248.                 dataAdapter.Fill(dataSet);
  249.                 dataGridView1.DataSource = dataSet.Tables[0];
  250.  
  251.                 SqlDataAdapter dataAdapter1 = new SqlDataAdapter($"SELECT Recordings.AlbumTitle,  Recordings.DateOfSale, Recordings.NumberOfSales FROM CatalogInMusicStore, Recordings, RecordCompanys WHERE CatalogInMusicStore.IdRecords = Recordings.IdRecord AND CatalogInMusicStore.IdRecordCompanys = RecordCompanys.IdRecordCompany AND RecordCompanys.RecordCompany = '{tb_RecordCompanys.Text}'", sqlConnection);
  252.  
  253.                 DataSet dataSet1 = new DataSet();
  254.  
  255.                 dataAdapter1.Fill(dataSet1);
  256.                 dataGridView2.DataSource = dataSet1.Tables[0];
  257.  
  258.                 SqlDataAdapter dataAdapter2 = new SqlDataAdapter($"SELECT InfoAboutPerformers.FirstName, InfoAboutPerformers.LastName, InfoAboutPerformers.DateOfBirth, InfoAboutPerformers.PlaceOfBirth, InfoAboutPerformers.Professions, InfoAboutPerformers.Groups FROM CatalogInMusicStore, InfoAboutPerformers, RecordCompanys WHERE CatalogInMusicStore.IdInformationAboutPerformers = InfoAboutPerformers.IdPerfomersInfo AND CatalogInMusicStore.IdRecordCompanys = RecordCompanys.IdRecordCompany AND RecordCompanys.RecordCompany = '{tb_RecordCompanys.Text}'", sqlConnection);
  259.  
  260.                 DataSet dataSet2 = new DataSet();
  261.  
  262.                 dataAdapter2.Fill(dataSet2);
  263.                 dataGridView3.DataSource = dataSet2.Tables[0];
  264.             }
  265.             else
  266.                 MessageBox.Show("Будь ласка, введіть дані!");
  267.         }
  268.  
  269.         private void button1_Click(object sender, EventArgs e)
  270.         {
  271.             SqlCommand cmd;
  272.             string cbox = comboBox1.SelectedItem.ToString();
  273.             if(cbox == "Кількість продаж")
  274.             {
  275.                 cmd = new SqlCommand("SELECT COUNT(select sum(CatalogInMusicStore.Quantity) from CatalogInMusicStore", sqlConnection);
  276.  
  277.                 int countSale = Convert.ToInt32(cmd.ExecuteScalar());
  278.                 cmd.Dispose();
  279.                 cmd.CommandText = "SELECT COUNT(select sum(CatalogInMusicStore.Quantity) from CatalogInMusicStore";
  280.                 //MessageBox.Show("Загальна кількість продаж: " + cmd.);
  281.             }
  282.             else if(cbox == "Кількість альбомів")
  283.             {
  284.                 cmd = new SqlCommand("SELECT COUNT(DISTINCT Recordings.AlbumTitle) FROM Recordings", sqlConnection);
  285.  
  286.                 int countAlbum = Convert.ToInt32(cmd.ExecuteScalar());
  287.                 cmd.Dispose();
  288.  
  289.                 MessageBox.Show("Загальна кількість альбомів: " + countAlbum.ToString());
  290.             }
  291.         }
  292.  
  293.  
  294.         private void button1_Click_1(object sender, EventArgs e)
  295.         {
  296.             SqlCommand cmd;
  297.             string cbox = comboBox1.SelectedItem.ToString();
  298.             if(cbox == "Кількість продаж")
  299.             {
  300.                 cmd = new SqlCommand("SELECT sum(CatalogInMusicStore.NumberOfSales) from CatalogInMusicStore", sqlConnection);
  301.  
  302.                 int countSale = Convert.ToInt32(cmd.ExecuteScalar());
  303.                 cmd.Dispose();
  304.                 //cmd.CommandText = "SELECT COUNT(select sum(CatalogInMusicStore.Quantity) from CatalogInMusicStore";
  305.                 MessageBox.Show("Загальна кількість продаж: " + countSale.ToString());
  306.             }
  307.             else if(cbox == "Кількість альбомів")
  308.             {
  309.                 cmd = new SqlCommand("SELECT COUNT(DISTINCT Recordings.AlbumTitle) FROM Recordings", sqlConnection);
  310.  
  311.                 int countAlbum = Convert.ToInt32(cmd.ExecuteScalar());
  312.                 cmd.Dispose();
  313.  
  314.                 MessageBox.Show("Загальна кількість альбомів: " + countAlbum.ToString());
  315.             }
  316.         }
  317.  
  318.         private void btn_Clean_Click(object sender, EventArgs e)
  319.         {
  320.  
  321.         }
  322.  
  323.         private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
  324.         {
  325.  
  326.         }
  327.  
  328.         private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
  329.         {
  330.  
  331.         }
  332.  
  333.         private void comboBox3_SelectedIndexChanged_1(object sender, EventArgs e)
  334.         {
  335.  
  336.             if(comboBox3.SelectedIndex == 0)
  337.             {
  338.                 SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from [Tools]", sqlConnection);
  339.  
  340.                 DataSet dataSet = new DataSet();
  341.  
  342.                 dataAdapter.Fill(dataSet);
  343.                 dataGridView4.DataSource = dataSet.Tables[0];
  344.  
  345.             }
  346.             if(comboBox3.SelectedIndex == 1)
  347.             {
  348.                 SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from [Customers]", sqlConnection);
  349.  
  350.                 DataSet dataSet = new DataSet();
  351.  
  352.                 dataAdapter.Fill(dataSet);
  353.                 dataGridView4.DataSource = dataSet.Tables[0];
  354.  
  355.             }
  356.  
  357.         }
  358.  
  359.         private void button2_Click(object sender, EventArgs e)
  360.         {
  361.             if(comboBox3.Text == "Ітструменти")
  362.             {
  363.                 MessageBox.Show("__");
  364.             }
  365.         }
  366.  
  367.         private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
  368.         {
  369.             //if(comboBox2.SelectedIndex == 0)
  370.             //{
  371.             //    MessageBox.Show(comboBox2.Text.ToString());
  372.             //    SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from [Tools]", sqlConnection);
  373.  
  374.             //    DataSet dataSet = new DataSet();
  375.  
  376.             //    dataAdapter.Fill(dataSet);
  377.             //    dataGridView4.DataSource = dataSet.Tables[0];
  378.  
  379.             //}
  380.         }
  381.  
  382.         private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
  383.         {
  384.             SqlDataAdapter dataAdapter = new SqlDataAdapter($"select Id as 'Код замовлення', IdTool as 'Код інструмента', NameTool as 'Назва інструмента' from [Order] join [Tools] on IdTool = ToolId where NameTool = N'{comboBox4.Text}'", sqlConnection);
  385.  
  386.             DataSet dataSet = new DataSet();
  387.  
  388.             dataAdapter.Fill(dataSet);
  389.             dataGridView4.DataSource = dataSet.Tables[0];
  390.         }
  391.     }
  392. }
  393.  
Advertisement
Add Comment
Please, Sign In to add comment