C# Access Kayıt Ekleme, Silme ve Güncelleme Örneği

C# access bağlantısı ile basit veri tabanı işlemleri(Ekle, Sil, Ara, Güncelle) yapalım form üzerindeki nesnelerimiz gibi oluşturduktan sonra kodlarımız aşağıdaki örnek ile aynı düzenleyelim. Form1_Load event durumuna kodları yapıştırın ve Bin / Debug içinde bulunan proje derlemesine accesss veritabanını oluşturun. Yukarıda form düzenimiz mevcuttur.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;  // Access bağlantısı kurabilmek için.
using System.Collections; //ArrayList kullanabilmek için.
namespace veribaglanti1
{
    public partial class Form1 : Form
    {
        //Access veritabanıına bağlantı sağlıyoruz
        OleDbConnection con;
        OleDbDataAdapter da;
        OleDbCommand cmd;
        OleDbDataReader dr;
        DataSet ds;

        public Form1()
        {
            InitializeComponent();
        }
        void griddoldur()
        {
            con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=okul.accdb");
            da = new OleDbDataAdapter("SElect *from ogrenci", con);
            ds = new DataSet();
            con.Open();
            da.Fill(ds, "ogrenci");
            dataGridView1.DataSource = ds.Tables["ogrenci"];
            con.Close();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            griddoldur();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //kayıt eklemek için
            cmd = new OleDbCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "insert into ogrenci (ogr_no,ogr_ad,ogr_soyad,ogr_tel) values ('" + tbno.Text + "','" + tbad.Text + "','" + tbsoyad.Text + "','" + tbtel.Text + "')";
            cmd.ExecuteNonQuery();
            con.Close();
            griddoldur();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Kayıt güncelemek için
            cmd = new OleDbCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "update ogrenci set ogr_ad='" + tbad.Text + "',ogr_soyad='" + tbsoyad.Text + "',ogr_tel='" + tbtel.Text + "' where ogr_no="+tbno.Text+"";
            cmd.ExecuteNonQuery();
            con.Close();
            griddoldur();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Kayıt silmek için
            cmd = new OleDbCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "delete from ogrenci where ogr_no="+tbno.Text+"";
            cmd.ExecuteNonQuery();
            con.Close();
            griddoldur();
        }

      
        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            tbno.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            tbad.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            tbsoyad.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            tbtel.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            //Kayıt aramak için textox
            con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=okul.accdb");
            da = new OleDbDataAdapter("SElect *from ogrenci where ogr_ad like '"+textBox5.Text+"%'", con);
            ds = new DataSet();
            con.Open();
            da.Fill(ds, "ogrenci");
            dataGridView1.DataSource = ds.Tables["ogrenci"];
            con.Close();
        }

        
        private void button5_Click(object sender, EventArgs e)
        {
            // Veriabanında bulunana toplam kayıtı saydırma
            int kayitSayisi;
            cmd = new OleDbCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "SELECT COUNT(*) FROM ogrenci";
            kayitSayisi =(int) cmd.ExecuteScalar() ;
            con.Close();
            MessageBox.Show(kayitSayisi.ToString());
        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //tüm kayıtları form açılışında direk buraya aktarır
        }

        private void tbno_TextChanged(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }
    }
}
Bu yazı C# kategorisine gönderilmiş ve , ile etiketlenmiş. Kalıcı bağlantıyı yer imlerinize ekleyin.

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir