19 Nisan 2020 Pazar

Kütüphane Projesi Cevap Anahtarı


OleDbConnection bağlantı=new OleDbConnection("Provider=Microsoft.Ace.OleDb.12.0;Data Source=Kütüphane.accdb");
DataTable Tablo = new DataTable();
OleDbCommand Komut = new OleDbCommand();

        private void Listele()
        {
            OleDbDataAdapter Kayıt = new OleDbDataAdapter ("Select * From Kitaplar", bağlantı);
            Tablo.Clear();
            Kayıt.Fill(Tablo);
            dataGridView1.DataSource = Tablo;
        }

        private void Okuma()
        {
            OleDbDataReader oku;
            comboBox1.Items.Clear();

            bağlantı.Open();
            OleDbCommand Komut = new OleDbCommand();
            Komut.Connection = bağlantı;
            Komut.CommandText = "Select * From Kitaplar";

            oku = Komut.ExecuteReader();
            while (oku.Read())
            {
                comboBox1.Items.Add(oku["Yayınevi"].ToString());
            }
            bağlantı.Close();
        }

        private void Temizle()
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
        }  

        private void Form1_Load(object sender, EventArgs e)
        {
            Listele();
            Okuma();
            //Hücreleri satır satır seçmek için
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bağlantı.Open();
            Komut.Connection = bağlantı;

            Komut.CommandText = 
            "INSERT INTO Kitaplar (KitapNo, KitapAdı, Yazarı, Yayınevi, Fiyatı, BasımYılı, SayfaSayısı, StokKodu) 
            VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "',   
            '" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')";

            Komut.ExecuteNonQuery();
            bağlantı.Close();
            Listele();
            Okuma();
            Temizle();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bağlantı.Open();
            Komut.Connection = bağlantı;

            Komut.CommandText = "DELETE From Kitaplar 
            WHERE KitapNo='" dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";

            Komut.ExecuteNonQuery();
            bağlantı.Close();
            Listele();
            Okuma();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            textBox4.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            textBox5.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            textBox6.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            textBox7.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            textBox8.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bağlantı.Open();

            Komut.Connection = bağlantı;

            Komut.CommandText = "UPDATE Kitaplar SET KitapNo='"+ textBox1.Text + "', KitapAdı='" + textBox2.Text +"', 
            Yazarı='" + textBox3.Text + "', Yayınevi='" +textBox4.Text + "', Fiyatı='" + textBox5.Text +"', BasımYılı='" + textBox6.Text + "', 
            SayfaSayısı='" +textBox7.Text + "' , StokKodu='" +textBox8.Text + "' 
            WHERE KitapNo='"+dataGridView1.CurrentRow.Cells[0].Value.ToString()+"'";

            Komut.ExecuteNonQuery();
            bağlantı.Close();
            Listele();
            Okuma();
        }

       private void ListeleKitapAdı()
        {
            OleDbDataAdapter Kayıt = new OleDbDataAdapter 
            ("Select * From Kitaplar WHERE KitapAdı Like '%" + textBox9.Text + "%'", bağlantı);

            Tablo.Clear();
            Kayıt.Fill(Tablo);
            dataGridView1.DataSource = Tablo;
        } 

        private void ListeleYazarı()
        {
            OleDbDataAdapter Kayıt = new OleDbDataAdapter 
            ("Select * From Kitaplar WHERE Yazarı Like '%" + textBox10.Text + "%'", bağlantı);

            Tablo.Clear();
            Kayıt.Fill(Tablo);
            dataGridView1.DataSource = Tablo;
        }

        private void ListeleYayıneviCombo()
        {
            OleDbDataAdapter Kayıt =
            new OleDbDataAdapter ("Select * From Kitaplar WHERE Yayınevi='" + comboBox1.Text + "'", bağlantı);

            Tablo.Clear();
            Kayıt.Fill(Tablo);
            dataGridView1.DataSource = Tablo;
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            if (textBox9.Text == "")
            {
                Listele();
            }
            else
            {
                ListeleKitapAdı();
            }
        } 
  
        private void textBox10_TextChanged(object sender, EventArgs e)
        {
            if (textBox10.Text == "")
            {
                Listele();
            }
            else
            {
                ListeleYazarı();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListeleYayıneviCombo();
        }       
    }
}



Hiç yorum yok:

Yorum Gönder