C# Access Veritabanı Bağlantısı
C# ile Access Veritabanı kullanımına birlikte bakalım . Benimde projelerimde kullandığım veri tabanı olan Access ile küçük bir uygulama yaparak açıklayalım

Örnek bir Form üstünden gidelim
Formumuzda 5 adet textbox,1 adet datagridview, 3 adet buton ve 5 adet Label bulunmaktadır.
Daha sonra “okul.accdb” isminde bir veritabanı dosyası oluşturarak projemizin “debug” klasörü içine atalım ve içine aşağıdaki şekilde “ogrenci” tablomuzu oluşturalım.
Geri kalanını Kodlar üzerinden açıklayalım
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 gerekli.
namespace veribaglanti1
{
public partial class Form1 : Form
{
OleDbConnection con;
OleDbDataAdapter da;
OleDbCommand cmd;
DataSet ds;
public Form1()
{
InitializeComponent();
}
void doldur() // Veri tabanını görüntülemek için
{
con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=okul.accdb"); // Veri tabanı yolu
da = new OleDbDataAdapter("SElect *from ogrenci", con); //Tablo ismi
ds = new DataSet();
con.Open();
da.Fill(ds, "ogrenci");
dataGridView1.DataSource = ds.Tables["ogrenci"]; // Tablo görüntülendi
con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
doldur(); //alt program çağrıldı
}
private void button1_Click(object sender, EventArgs e) // EKLE BUTONU
{
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 + "')"; //Veri tabanına eklemek için önce tablo ismi sonra Textboxlar
cmd.ExecuteNonQuery();
con.Close();
doldur();
}
private void button3_Click(object sender, EventArgs e) //GÜNCELLE BUTONU
{
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+""; //veritabanı adı = İçerik
cmd.ExecuteNonQuery();
con.Close();
doldur();
}
private void button2_Click(object sender, EventArgs e) //SİLME BUTONU
{
cmd = new OleDbCommand();
con.Open();
cmd.Connection = con;
cmd.CommandText = "delete from ogrenci where ogr_no="+tbno.Text+""; // Silinecek İçerik İdsi where arama için
cmd.ExecuteNonQuery();
con.Close();
doldur();
}
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) //DATAGRİDVİEW SATIRA TIKLANDIĞINDA TEXTBOXLARA AKTARMA
{
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) // ARAMA YAPMA
{
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); // where neye göre arama yapılacak onu belirtir
ds = new DataSet();
con.Open();
da.Fill(ds, "ogrenci");
dataGridView1.DataSource = ds.Tables["ogrenci"];
con.Close();
}
}
}
Gerekli açıklamaları Kodların yanına yazarak açıklamaya çalıştım . Takıldığınız yerde yorum yazarak yardımcı olabiliriz.
