5 Nisan 2020 Pazar

Visual C# Veritabanı İşlemleri Kısa-Özet

Sevgili Gençler, yeniden kucak dolusu Merhabalar...
Bu hafta geçen hafta yaptığımız çalışmanın kısaltılmış halini size sunacağım.
Böylece benzer ifadelerin tekrar tekrar yazılmasına gerek kalmayacak.


using System.Data.OleDb;


OleDbConnection bağlantı = new OleDbConnection ("Provider = 
Microsoft.Ace.OleDb.12.0; Data Source= Ajanda.accdb");

DataTable Tablo = new DataTable();

OleDbCommand Komut = new OleDbCommand();

private void Listele()
{
    OleDbDataAdapter Kayıt = new OleDbDataAdapter 
    ("Select * From Rehber", 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 Rehber";

    oku = Komut.ExecuteReader();
    while (oku.Read())
    {
        comboBox1.Items.Add(oku["OkulNo"].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 Rehber(OkulNo,AdıSoyadı,Adres,Telefon) 
    VALUES('" + textBox1.Text + "','" + textBox2.Text + "',
    '"+ textBox3.Text + "','" + textBox4.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 Rehber WHERE 
    OkulNo='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";

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

private void dataGridView1_DoubleClick(object sender, EventArgs 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();
}

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

    Komut.Connection = bağlantı;

    Komut.CommandText = "UPDATE Rehber  
    SET OkulNo='"+ textBox1.Text + "', AdıSoyadı='" + textBox2.Text +"',      
    Adres='" + textBox3.Text + "', Telefon='" +textBox4.Text + "' 
    WHERE OkulNo='"+dataGridView1.CurrentRow.Cells[0].Value.ToString()+"'";

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

private void ListeleNumara()
{
    OleDbDataAdapter Kayıt = new OleDbDataAdapter
    ("Select * From Rehber WHERE OkulNo='" + textBox5.Text + "'", bağlantı);
    
    Tablo.Clear();
    Kayıt.Fill(Tablo);
    dataGridView1.DataSource = Tablo;
}

private void ListeleAdıSoyadı()
{
    OleDbDataAdapter Kayıt = new OleDbDataAdapter("Select * From Rehber 
    WHERE AdıSoyadı Like '%" + textBox6.Text + "%'", bağlantı);

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

private void ListeleNumaraCombo()
{
    OleDbDataAdapter Kayıt =new OleDbDataAdapter ("Select * From Rehber 
    WHERE OkulNo='" + comboBox1.Text + "'", bağlantı);

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

private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar==13)
    {
        if (textBox5.Text == "")
    {
        Listele();
    }
    else
    {
        ListeleNumara();
    } 
    }
}

private void textBox6_TextChanged(object sender, EventArgs e)
{
    if (textBox6.Text == "")
    {
        Listele();
    }
    else
    {
        ListeleAdıSoyadı();
    } 
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ListeleNumaraCombo();
}   

Sağlıklı günlerde görüşmek üzere...

Hiç yorum yok:

Yorum Gönder