1-) C# - Bölüm 14 Access Veri Tabanı
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;
namespace Kitaplik_Proje
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection baglanti = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\FT_bu\Desktop\Kitaplik.mdb");
void listele()
{
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter("select * from kitaplar", baglanti);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void Form1_Load(object sender, EventArgs e)
{
listele();
}
string durum = "";
private void BtnListele_Click(object sender, EventArgs e)
{
listele();
}
private void BtnKaydet_Click(object sender, EventArgs e)
{
baglanti.Open();
OleDbCommand komut1 = new OleDbCommand("insert into Kitaplar (kitapad,yazar,tur,sayfa,durum) values(@p1,@p2,@p3,@p4,@p5)", baglanti);
komut1.Parameters.AddWithValue("@p1", TxtKitapAd.Text);
komut1.Parameters.AddWithValue("@p2", TxtYazar.Text);
komut1.Parameters.AddWithValue("@p3", CmbTur.Text);
komut1.Parameters.AddWithValue("@p4", TxtSayfa.Text);
komut1.Parameters.AddWithValue("@p5", durum);
komut1.ExecuteNonQuery();
baglanti.Close();
MessageBox.Show("Kitap Kaydedildi", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
listele();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
durum = "0";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
durum = "1";
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int secilen = dataGridView1.SelectedCells[0].RowIndex;
TxtKitapid.Text = dataGridView1.Rows[secilen].Cells[0].Value.ToString();
TxtKitapAd.Text = dataGridView1.Rows[secilen].Cells[1].Value.ToString();
TxtYazar.Text = dataGridView1.Rows[secilen].Cells[2].Value.ToString();
CmbTur.Text = dataGridView1.Rows[secilen].Cells[3].Value.ToString();
TxtSayfa.Text = dataGridView1.Rows[secilen].Cells[4].Value.ToString();
if (dataGridView1.Rows[secilen].Cells[5].Value.ToString() == "True")
{
radioButton2.Checked = true;
}
else
{
radioButton1.Checked = true;
}
}
private void BtnSil_Click(object sender, EventArgs e)
{
baglanti.Open();
OleDbCommand komut = new OleDbCommand("delete from kitaplar where kitapid=@p1", baglanti);
komut.Parameters.AddWithValue("@p1", TxtKitapid.Text);
komut.ExecuteNonQuery();
baglanti.Close();
MessageBox.Show("Kitap Listeden Silindi", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
listele();
}
private void BtnGuncelle_Click(object sender, EventArgs e)
{
baglanti.Open();
OleDbCommand komut = new OleDbCommand("update kitaplar set kitapad=@p1,yazar=@p2,tur=@p3,sayfa=@p4,durum=@p5 where kitapid=@p6", baglanti);
komut.Parameters.AddWithValue("@p1", TxtKitapAd.Text);
komut.Parameters.AddWithValue("@p2", TxtYazar.Text);
komut.Parameters.AddWithValue("@p3", CmbTur.Text);
komut.Parameters.AddWithValue("@p4", TxtSayfa.Text);
if (radioButton1.Checked == true)
{
komut.Parameters.AddWithValue("@p5", durum);
}
if (radioButton2.Checked == true)
{
komut.Parameters.AddWithValue("@p5", durum);
}
komut.Parameters.AddWithValue("@p6", TxtKitapid.Text);
komut.ExecuteNonQuery();
baglanti.Close();
MessageBox.Show("Kayıt Güncellendi", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Information);
listele();
}
private void BtnBul_Click(object sender, EventArgs e)
{
OleDbCommand komut = new OleDbCommand("select * from kitaplar where kitapad=@p1", baglanti);
komut.Parameters.AddWithValue("@p1", TxtKitapBul.Text);
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(komut);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void BtnAra_Click(object sender, EventArgs e)
{
OleDbCommand komut = new OleDbCommand("select * from kitaplar where kitapad like '%" + TxtKitapBul.Text + "%'", baglanti);
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(komut);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}