📅 19 Nisan 2026, Pazar
📚 Ders Kitapları ✉ İletişim
@ydiner.gen.tr İlköğretim & Lise Düzeyi Kaynak Site "Bilgi paylaştıkça çoğalır"
🏠 Ana Sayfa
  • 🧊 3D Tasarım
  • Animasyon
  • ⚡ Arduino
  • 📌 Genel
  • 🎨 Grafik
  • Pardus
  • 💻 Programlama
  • Robotik ve Kodlama
  • 🗄️ SQL
  • 🌐 Web Tasarım
  • 🤖 Yapay Zeka
  • Genel

    kütüphane

    14 Nisan 2026 aydiner

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace kutuphane
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    SqlConnection baglanti = new SqlConnection(@”Data Source=LAB3OGRTMEN\SQLEXPRESS01;Initial Catalog=KutuphaneDB;Integrated Security=True”);

    string secilenResimYolu = “”;

    void Listele()
    {
    try
    {
    baglanti.Open();
    SqlDataAdapter da = new SqlDataAdapter(“SELECT * FROM Kitaplar”, baglanti);
    DataTable dt = new DataTable();
    da.Fill(dt);
    dataGridView1.DataSource = dt;
    baglanti.Close();
    }
    catch (Exception ex)
    {
    MessageBox.Show(“Hata: ” + ex.Message);
    baglanti.Close();
    }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    Listele();

    // Satırların genel görünümü
    dataGridView1.BorderStyle = BorderStyle.None;
    dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(238, 239, 249); // Bir dolu bir boş satır rengi
    dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.DarkTurquoise; // Seçili satır rengi
    dataGridView1.DefaultCellStyle.SelectionForeColor = Color.WhiteSmoke;
    dataGridView1.BackgroundColor = Color.White;

    // Başlık (Header) tasarımı
    dataGridView1.EnableHeadersVisualStyles = false;
    dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
    dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(20, 25, 72); // Başlık arka plan (Lacivert)
    dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White; // Başlık yazı rengi
    dataGridView1.ColumnHeadersHeight = 40; // Başlık yüksekliği

    // Satır yüksekliği ve yazı tipi
    dataGridView1.RowTemplate.Height = 35;
    dataGridView1.DefaultCellStyle.Font = new Font(“Segoe UI”, 10);

    // Otomatik boyutlandırma
    dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // Sütunları genişliğe yay

     

     

     

     

    // Proje içinde Resimler klasörü yoksa oluşturur
    string resimKlasoru = Path.Combine(Application.StartupPath, “Resimler”);
    if (!Directory.Exists(resimKlasoru))
    {
    Directory.CreateDirectory(resimKlasoru);
    }
    }

    private void btnResimSec_Click(object sender, EventArgs e)
    {
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = “Resim Dosyaları (*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png”;

    if (ofd.ShowDialog() == DialogResult.OK)
    {
    pictureBox1.ImageLocation = ofd.FileName;
    secilenResimYolu = ofd.FileName;
    }
    }

    private void button1_Click(object sender, EventArgs e)
    {

    try
    {
    string yeniResimYolu = “”;

    // Eğer resim seçildiyse kopyalama yap
    if (!string.IsNullOrEmpty(secilenResimYolu))
    {
    string dosyaAdi = Guid.NewGuid().ToString() + Path.GetExtension(secilenResimYolu); // İsim çakışması olmaması için benzersiz isim
    yeniResimYolu = Path.Combine(Application.StartupPath, “Resimler”, dosyaAdi);
    File.Copy(secilenResimYolu, yeniResimYolu);
    }

    baglanti.Open();
    SqlCommand komut = new SqlCommand(“INSERT INTO Kitaplar (KitapAd, Yazar, SayfaSayisi, Ozet, KitapKapakYolu, ISBN, RafYeri, Adet) VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8)”, baglanti);

    komut.Parameters.AddWithValue(“@p1”, txtKitapAd.Text);
    komut.Parameters.AddWithValue(“@p2”, txtYazar.Text);
    komut.Parameters.AddWithValue(“@p3”, string.IsNullOrEmpty(txtSayfa.Text) ? 0 : int.Parse(txtSayfa.Text));
    komut.Parameters.AddWithValue(“@p4”, txtOzet.Text);
    komut.Parameters.AddWithValue(“@p5”, yeniResimYolu); // Kopyalanan yeni adres
    komut.Parameters.AddWithValue(“@p6”, txtISBN.Text);
    komut.Parameters.AddWithValue(“@p7”, txtRaf.Text);
    komut.Parameters.AddWithValue(“@p8”, string.IsNullOrEmpty(txtAdet.Text) ? 0 : int.Parse(txtAdet.Text));

    komut.ExecuteNonQuery();
    baglanti.Close();

    MessageBox.Show(“Kitap başarıyla kaydedildi!”);
    Listele();
    Temizle();
    }
    catch (Exception ex)
    {
    MessageBox.Show(“Hata oluştu: ” + ex.Message);
    baglanti.Close();
    }
    }

    void Temizle()
    {
    txtKitapAd.Clear();
    txtYazar.Clear();
    txtSayfa.Clear();
    txtOzet.Clear();
    txtISBN.Clear();
    txtRaf.Clear();
    txtAdet.Clear();
    pictureBox1.Image = null;
    secilenResimYolu = “”;
    }

     

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
    int secilen = dataGridView1.SelectedCells[0].RowIndex;

    txtKitapAd.Text = dataGridView1.Rows[secilen].Cells[1].Value.ToString();
    txtYazar.Text = dataGridView1.Rows[secilen].Cells[2].Value.ToString();
    txtOzet.Text = dataGridView1.Rows[secilen].Cells[4].Value.ToString();

    string resimYolu = dataGridView1.Rows[secilen].Cells[5].Value.ToString();
    if (File.Exists(resimYolu))
    {
    pictureBox1.ImageLocation = resimYolu;
    }
    else
    {
    pictureBox1.Image = null;
    }
    }

    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {

    // HATA ÖNLEYİCİ KONTROL:
    // Eğer satır seçili değilse veya satır sayısı 0 ise işlem yapma ve metoddan çık
    if (dataGridView1.CurrentRow == null || dataGridView1.SelectedRows.Count == 0)
    {
    return;
    }

    try
    {
    DataGridViewRow satir = dataGridView1.SelectedRows[0];

    // Hücre değerlerini alırken boş (null) kontrolü eklemek daha güvenlidir
    txtKitapAd.Text = satir.Cells[“KitapAd”].Value?.ToString();
    txtYazar.Text = satir.Cells[“Yazar”].Value?.ToString();
    txtSayfa.Text = satir.Cells[“SayfaSayisi”].Value?.ToString();
    txtOzet.Text = satir.Cells[“Ozet”].Value?.ToString();
    txtISBN.Text = satir.Cells[“ISBN”].Value?.ToString();
    txtRaf.Text = satir.Cells[“RafYeri”].Value?.ToString();
    txtAdet.Text = satir.Cells[“Adet”].Value?.ToString();

    string resimYolu = satir.Cells[“KitapKapakYolu”].Value?.ToString();
    if (!string.IsNullOrEmpty(resimYolu) && File.Exists(resimYolu))
    {
    pictureBox1.ImageLocation = resimYolu;
    }
    else
    {
    pictureBox1.Image = null;
    }
    }
    catch (Exception)
    {
    // Hata durumunda programın çökmesini engellemek için boş bırakılabilir
    }
    }

    private void txtArama_TextChanged(object sender, EventArgs e)
    {
    // Arama kutusu boşsa tüm listeyi getir, doluysa filtrele
    try
    {
    if (baglanti.State == ConnectionState.Closed)
    baglanti.Open();

    // SQL’deki “LIKE” operatörü ile içinde geçen kelimeleri arıyoruz
    // ‘%’ işareti aranan kelimenin başında veya sonunda herhangi bir karakter olabileceğini söyler
    string sorgu = “SELECT * FROM Kitaplar WHERE KitapAd LIKE @p1 OR Yazar LIKE @p1”;

    SqlDataAdapter da = new SqlDataAdapter(sorgu, baglanti);
    da.SelectCommand.Parameters.AddWithValue(“@p1”, “%” + txtArama.Text + “%”);

    DataTable dt = new DataTable();
    da.Fill(dt);
    dataGridView1.DataSource = dt;

    baglanti.Close();
    }
    catch (Exception ex)
    {
    MessageBox.Show(“Arama sırasında hata oluştu: ” + ex.Message);
    if (baglanti.State == ConnectionState.Open)
    baglanti.Close();
    }
    }

    private void button2_Click(object sender, EventArgs e)
    {

    // 1. Kontrol: Seçili satır var mı?
    if (dataGridView1.SelectedRows.Count > 0)
    {
    // Kullanıcıdan onay alalım
    DialogResult onay = MessageBox.Show(“Bu kitabı silmek istediğinize emin misiniz?”, “Silme Onayı”, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

    if (onay == DialogResult.Yes)
    {
    try
    {
    // Seçili satırdaki KitapID değerini alıyoruz (Sütun adı veya indeksi doğru olmalı)
    int seciliId = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[“KitapID”].Value);

    baglanti.Open();
    SqlCommand komutSil = new SqlCommand(“DELETE FROM Kitaplar WHERE KitapID = @p1”, baglanti);
    komutSil.Parameters.AddWithValue(“@p1”, seciliId);

    komutSil.ExecuteNonQuery();
    baglanti.Close();

    MessageBox.Show(“Kitap başarıyla silindi.”);

    // Listeyi yenile ve kutuları temizle
    Listele();
    Temizle();
    }
    catch (Exception ex)
    {
    MessageBox.Show(“Silme hatası: ” + ex.Message);
    baglanti.Close();
    }
    }
    }
    else
    {
    MessageBox.Show(“Lütfen silmek istediğiniz kitabı tablodan seçin.”);
    }
    }

    private void button4_Click(object sender, EventArgs e)
    {
    txtKitapAd.Clear();
    txtYazar.Clear();
    txtOzet.Clear();
    txtRaf.Clear();
    txtSayfa.Clear();
    txtISBN.Clear();
    txtAdet.Clear();
    txtKitapAd.Focus();
    pictureBox1.Image=null;

    }

    private void button3_Click(object sender, EventArgs e)
    {
    if (dataGridView1.SelectedRows.Count > 0)
    {
    try
    {
    baglanti.Open();
    // SQL Sorgusu: Sadece seçili ID’ye sahip olan kitabı günceller
    string sorgu = “UPDATE Kitaplar SET KitapAd=@p1, Yazar=@p2, SayfaSayisi=@p3, Ozet=@p4, ISBN=@p5, RafYeri=@p6, Adet=@p7 WHERE KitapID=@p0”;

    SqlCommand komutGuncelle = new SqlCommand(sorgu, baglanti);

    // Değerleri parametre olarak ekliyoruz
    komutGuncelle.Parameters.AddWithValue(“@p1”, txtKitapAd.Text);
    komutGuncelle.Parameters.AddWithValue(“@p2”, txtYazar.Text);
    komutGuncelle.Parameters.AddWithValue(“@p3”, int.Parse(txtSayfa.Text));
    komutGuncelle.Parameters.AddWithValue(“@p4”, txtOzet.Text);
    komutGuncelle.Parameters.AddWithValue(“@p5”, txtISBN.Text);
    komutGuncelle.Parameters.AddWithValue(“@p6”, txtRaf.Text);
    komutGuncelle.Parameters.AddWithValue(“@p7”, int.Parse(txtAdet.Text));

    // Hangi satırın güncelleneceğini belirleyen ID (SelectionChanged ile gelen ID)
    int seciliId = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[“KitapID”].Value);
    komutGuncelle.Parameters.AddWithValue(“@p0”, seciliId);

    komutGuncelle.ExecuteNonQuery();
    baglanti.Close();

    MessageBox.Show(“Kitap bilgileri başarıyla güncellendi!”);
    Listele(); // Tabloyu yenile
    }
    catch (Exception ex)
    {
    MessageBox.Show(“Güncelleme hatası: ” + ex.Message);
    baglanti.Close();
    }
    }
    else
    {
    MessageBox.Show(“Lütfen güncellemek istediğiniz kitabı listeden seçin!”);
    }
    }

    private void button5_Click(object sender, EventArgs e)
    {
    Application.Exit();
    }
    }
    }