none
Přesunutí logiky do samostatné třídy

    Dotaz

  • Dobrý den,

    potřeboval bych přesunout logiku do samostatné třídy co se mi opakuje (tučně), mění se mi pouze sql příkaz:

            private void btnZakSmazat_Click(object sender, EventArgs e)
            {
                
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TridniKnihaConnectionString"].ConnectionString))
                {
                    con.Open();
    
                    SqlCommand command = new SqlCommand("delete from Zak where ID=" +_idVybranehoZaka, con);
                    command.ExecuteNonQuery();
                }
    
                NactiData();
                NactiHodnotyDoFormulare();
                
            }

    Celý kód:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using TridniKniha.Entity;
    using System.Configuration;
    
    namespace TridniKniha.Formulare
    {
        public partial class TridniKnihaForm : Form
        {
            private int _idVybranehoZaka;
    
            private List<Zak> _zaci;
            private List<Predmet> _predmety;
    
            public TridniKnihaForm()
            {
                NactiData();
    
                InitializeComponent();
    
                NactiHodnotyDoFormulare();
    
                SchovejPanely();
            }
    
            private void SchovejPanely()
            {
                gpbDochazka.Visible = false;
                gpbZaci.Visible = false;
            }
    
            private void NactiData()
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TridniKnihaConnectionString"].ConnectionString))
                {
                    con.Open();
    
                    SqlCommand command = new SqlCommand("select * from Predmet", con);
                    SqlDataReader reader = command.ExecuteReader();
    
                    _predmety = new List<Predmet>();
    
                    while (reader.Read())
                    {
                        _predmety.Add(new Predmet
                        {
                            ID = reader.GetInt32(0),
                            Nazev = reader.GetString(1),
                        });
                    }
    
                    reader.Close();
    
                    command.CommandText = "select * from Zak";
    
                    reader = command.ExecuteReader();
    
                    _zaci = new List<Zak>();
    
                    while (reader.Read())
                    {
                        _zaci.Add(new Zak
                        {
                            ID = reader.GetInt32(0),
                            Jmeno = reader.GetString(1),
                            Prijmeni = reader.GetString(2),
                        });
                    }
                }
            }
            
            //načtení předmětů do formuláře
            private void NactiHodnotyDoFormulare()
            {
                cmbPredmet.DataSource = _predmety;
                cmbPredmet.DisplayMember = "Nazev";
                cmbPredmet.ValueMember = "ID";
    
                cmbTyden.Items.Clear();
                for (int i = 1; i < 13; i++)
                {
                    cmbTyden.Items.Add(i);
                }
    
                lstZaci.DataSource = _zaci;
                lstZaci.DisplayMember = "Prijmeni";
                lstZaci.ValueMember = "ID";
            }
    
            //výběr zvoleného týdne
            private void cmbTyden_SelectedIndexChanged(object sender, EventArgs e)
            {
    			if (cmbTyden.SelectedIndex < 0) return;
    
                chbDochazka.DataSource = _zaci;
                chbDochazka.DisplayMember = "Prijmeni";
                chbDochazka.ValueMember = "ID";
    
                //nastavení všech checkbox na vynulováno
                for (int i = 0; i < chbDochazka.Items.Count; i++)
                {
                    chbDochazka.SetItemChecked(i, false);
                }
    
                //vyběr přítomného studenta
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TridniKnihaConnectionString"].ConnectionString))
                {
                    con.Open();
    
                    var vybranyPredmet = (Predmet)cmbPredmet.SelectedItem;
    				int vybranyTyden = Convert.ToInt32(cmbTyden.Text);
    
                    List<int> vybraniZaci = new List<int>();
    
    				var command = new SqlCommand("select IDZAKA from Dochazka where IDPREDMETU = " + vybranyPredmet.ID + " and TYDEN = " + vybranyTyden, con);
                    using (var reader = command.ExecuteReader())
                    { 
                        while(reader.Read())
                        {
                            int id = reader.GetInt32(0);
    						int index = -1;
    
                            foreach (Zak item in chbDochazka.Items)
                            {
    							index++;
    
                                if (item.ID != id) continue;
    
                                vybraniZaci.Add(index);
    
    							break;
                            }
    
    						index = -1;
                        }
                    }
    
                    //vybrání žáka v docházce
                    foreach (int index in vybraniZaci)
    	            {
                        chbDochazka.SetItemChecked(index, true);
    	            }
                }
            }
    
            private void btnUlozitDochazku_Click(object sender, EventArgs e)
            {
                var vybranyPredmet = (Predmet)cmbPredmet.SelectedItem;
    			int vybranyTyden = Convert.ToInt32(cmbTyden.Text);
    
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TridniKnihaConnectionString"].ConnectionString))
                {
                    con.Open();
    
                    foreach (var item in chbDochazka.CheckedItems)
                    {
                        var zak = (Zak)item;
    
    					SqlCommand command = new SqlCommand("select top 1 ID from Dochazka where IDZAKA = " + zak.ID + " and IDPREDMETU = " + vybranyPredmet.ID + " and TYDEN = " + vybranyTyden, con);
    
                        int dochazkaPocet = 0;
                        using ( var reader = command.ExecuteReader())
                        {
                            while(reader.Read())
                            {
                                dochazkaPocet = reader.GetInt32(0);
                            }
                        }
    
                        if (dochazkaPocet <= 0)
                        {
                            SqlCommand command2 = new SqlCommand("insert into Dochazka(IDZAKA, IDPREDMETU, TYDEN) values(" + zak.ID + ", " + vybranyPredmet.ID + ", " + vybranyTyden + ")", con);
                            int count = command2.ExecuteNonQuery();
                        }
    					
    					
                    }
                }
            }
    
            private void btnZaci_Click(object sender, EventArgs e)
            {
                SchovejPanely();
                gpbZaci.Visible = true;
            }
    
            private void btnDochazka_Click(object sender, EventArgs e)
            {
                SchovejPanely();
                gpbDochazka.Visible = true;
            }
    
            private void lstZaci_SelectedIndexChanged(object sender, EventArgs e)
            {
                var vybranyZak = (Zak)lstZaci.SelectedItem;
    
                tbZakJmeno.Text = vybranyZak.Jmeno;
                tbZakPrijmeni.Text = vybranyZak.Prijmeni;
    
                _idVybranehoZaka = vybranyZak.ID;
            }
    
            private void btnZakAktualizovat_Click(object sender, EventArgs e)
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TridniKnihaConnectionString"].ConnectionString))
                {
                    con.Open();
    
                    SqlCommand command = new SqlCommand("update Zak set JMENO = '" + tbZakJmeno.Text + "', PRIJMENI = '" + tbZakPrijmeni.Text + "' where ID = " + _idVybranehoZaka, con);
                    command.ExecuteNonQuery();
                }
    
                NactiData();
                NactiHodnotyDoFormulare();
            }
    
            //vytvoření nového žáka
            private void btnZakNovy_Click(object sender, EventArgs e)
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TridniKnihaConnectionString"].ConnectionString))
                {
                    con.Open();
    
                    SqlCommand command = new SqlCommand("insert into Zak(JMENO, PRIJMENI) values('" + tbZakJmeno.Text + "', '" + tbZakPrijmeni.Text + "')", con);
                    command.ExecuteNonQuery();
                }
    
                NactiData();
                NactiHodnotyDoFormulare();
            }
    
    
            private void btnZakSmazat_Click(object sender, EventArgs e)
            {
                
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TridniKnihaConnectionString"].ConnectionString))
                {
                    con.Open();
    
                    SqlCommand command = new SqlCommand("delete from Zak where ID=" +_idVybranehoZaka, con);
                    command.ExecuteNonQuery();
                }
    
                NactiData();
                NactiHodnotyDoFormulare();
                
            }
        }
    }
    

    Prosím co kam přesunout a jak to volat? :) díky moc

    10. ledna 2014 8:38