C# Console Veritabanı (Access) İşlemleri (Select-Insert-Update-Delete)
0

Şimdiye kadarki C# uygulamalarımızda veritabanı işlemlerini hep Form üzerinden yapıyorduk. Bu örnek de C# Console ile veritabanı üzerinde (Select-Insert-Update-Delete) işlemlerini gerçekleştireceğiz ilk olarak aşağıdaki özelliklerde bir tablo oluşturuyoruz. Örneğimiz için Veritabanı ismi okul tablomuz ise ogrenci olacak

Daha sonra Projemize

using System.Data;
using System.Data.OleDb;
using System.IO;

namespacelerini ekliyoruz

C# Console işlemleri için kullanacağımız kodlar..:

 static void Main(string[] args)
        {
           
            Console.WriteLine("\t Öğrenci Takip Sistemi");
            Console.WriteLine("1-Listele");
            Console.WriteLine("2-Ekle");
            Console.WriteLine("3-Güncelle");
            Console.WriteLine("4-Sil");
            Console.WriteLine("5-Çıkış");
            Console.Write("Seçiminiz [1..5]");
            int secim = 0;
            try
            {
                 secim = Convert.ToInt32(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("Hatalı Seçim Lütfen Tekrar Deneyin....");
            }
            if(secim==1)
            {
                KayitGetir();
            }
            else if(secim==2)
            {//http://www.bilisimogretmeni.com
                KayitEkle();
            }
            else if(secim==4)
            {
                KayitSil();
            }
            else if (secim ==3)
            {
                KayitGuncelle();
            }
            Console.ReadKey();          
        }
        public static void KayitGetir()
        {
            string klasor = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + klasor + "/okul.accdb;Persist Security Info=False");
            OleDbCommand komut = new OleDbCommand();
            OleDbDataAdapter adaptor = new OleDbDataAdapter();           
            if (baglanti.State == ConnectionState.Closed)
            baglanti.Open();
            komut = new OleDbCommand("select * from ogrenci", baglanti);
            OleDbDataReader bilgigetir = komut.ExecuteReader();
            Console.WriteLine("\n\nAdı\t Soyadı\t Sınıfı\t Numarası");
            int toplamkayit = 0;
            while (bilgigetir.Read())
            {
                toplamkayit++;//http://www.bilisimogretmeni.com
                Console.WriteLine(bilgigetir[1] + "\t" + bilgigetir[2] + "\t" + bilgigetir[3] + "\t" + bilgigetir[4]);
            }
            Console.WriteLine("Toplam Kayıt..:" + toplamkayit.ToString());
            baglanti.Close();
        }
        public static void KayitEkle()
        {
            Console.Write("\n\nÖğrencinin Adı\t\t..:");
            string ad = Console.ReadLine();
            Console.Write("Öğrencinin Soyadı\t..:");
            string soyad = Console.ReadLine();
            Console.Write("Öğrencinin Sınıfı\t..:");
            string sinif = Console.ReadLine();
            Console.Write("Öğrencinin Numarası\t..:");
            string numara = Console.ReadLine();
            string klasor = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + klasor + "/okul.accdb;Persist Security Info=False");
            OleDbCommand komut = new OleDbCommand();
            OleDbDataAdapter adaptor = new OleDbDataAdapter();           
            if (baglanti.State == ConnectionState.Closed)
                baglanti.Open();//http://www.bilisimogretmeni.com
            komut = new OleDbCommand("INSERT INTO ogrenci (adi,soyadi,sinif,numara) VALUES ('" + ad + "','" + soyad + "','" + sinif +"','"+numara+ "')", baglanti);         
            int sonuc = komut.ExecuteNonQuery();
            baglanti.Close();
            if (sonuc > 0)
            {
                Console.WriteLine("Kayıt Eklendi");
            }
            else
            {
                Console.WriteLine("İşlem Başarısız");
            }
        }
        public static void KayitSil()
        {
            Console.Write("\n\nKaydı Silinecek Öğrencinin Numarası");
            int silinecek = Convert.ToInt32(Console.ReadLine());
            string klasor = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + klasor + "/okul.accdb;Persist Security Info=False");
            OleDbCommand komut = new OleDbCommand();
            OleDbDataAdapter adaptor = new OleDbDataAdapter();
            if (baglanti.State == ConnectionState.Closed)
                baglanti.Open();
            komut = new OleDbCommand("Delete from ogrenci Where numara="+silinecek, baglanti);
            int sonuc = komut.ExecuteNonQuery();
            baglanti.Close();
            if (sonuc > 0)//http://www.bilisimogretmeni.com
            {
                Console.WriteLine("Kayıt Silindi");
            }
            else
            {
                Console.WriteLine("İşlem Başarısız");
            }
        }
        public static void KayitGuncelle()
        {
            Console.Write("\n\nÖğrencinin Adı\t\t..:");
            string ad = Console.ReadLine();
            Console.Write("Öğrencinin Soyadı\t..:");
            string soyad = Console.ReadLine();
            Console.Write("Öğrencinin Sınıfı\t..:");
            string sinif = Console.ReadLine();
            Console.Write("Öğrencinin Numarası\t..:");
            string numara = Console.ReadLine();
            string klasor = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + klasor + "/okul.accdb;Persist Security Info=False");
            OleDbCommand komut = new OleDbCommand();
            OleDbDataAdapter adaptor = new OleDbDataAdapter();
            if (baglanti.State == ConnectionState.Closed)
                baglanti.Open();
            komut = new OleDbCommand("UPDATE ogrenci SET adi='" + ad + "',soyadi='" + soyad + "',sinif='" + sinif + "',numara=" + numara + " WHERE numara=" + numara, baglanti);
            int sonuc = komut.ExecuteNonQuery();
            baglanti.Close();//http://www.bilisimogretmeni.com
            if (sonuc > 0)
            {
                Console.WriteLine("Kayıt Güncellendi");
            }
            else
            {
                Console.WriteLine("İşlem Başarısız");
            }
        }
Bu İçeriğe Tepkin Ne Oldu?
  • 4
    ba_ar_l_
    Başarılı
  • 0
    gayet_yi
    Gayet İyi
  • 1
    te_ekk_rler
    Teşekkürler
  • 0
    anlamad_m
    Anlamadım
  • 0
    yetersiz
    Yetersiz
Subscribe
Bildir
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Yorum
Inline Feedbacks
View all comments