7 Kasım 2012 Çarşamba

C# Veri Tabanı ve DataGrid İşlemleri


C# Veri Tabanı ve DataGrid İşlemleri

Programın Görüntüsü

Programın Kodları
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Veri_Tabani_İslemeri
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public OleDbConnection bag = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=vt1.mdb");
public OleDbCommand kmt = new OleDbCommand();
public OleDbDataAdapter adtr = new OleDbDataAdapter();
public DataSet dtst = new DataSet();
string guncelle = "";//Bunu Güncellemede Kullanıcaz
//-------------------------------------------------------------------------------------------------
public void listele()//DataGrid' e verileri çekmek için bu kodlar çalışacak.
{
bag.Open();
OleDbDataAdapter adtr = new OleDbDataAdapter("select * From Tablo1", bag);
adtr.Fill(dtst, "Tablo1");
dataGridView1.DataSource = dtst.Tables["Tablo1"];
adtr.Dispose();
bag.Close();
}
//-------------------------------------------------------------------------------------------------
public void kayitsayisi()//Kayıt Sayısını Göstermek İçin Bu kodları Çalışıcak.
{
bag.Open();
DataSet dtst = new DataSet();
string sorgu = "SELECT * FROM Tablo1";
OleDbDataAdapter adtr = new OleDbDataAdapter(sorgu, bag);
bag.Close();
adtr.Fill(dtst, "Tablo1");
dataGridView1.DataSource = dtst.Tables[0];
for (int i = 0; i < dataGridView1.RowCount; i++)
{
label7.Text = Convert.ToString(i);
}
}
//-------------------------------------------------------------------------------------------------
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//Burda DataGrid üzerine tıklandığında textBox' lara veriler aktarılıyor.
//Try Catch yapısına koymamın nedeni Datagrid'in veri değilde başka yerlerine tıklandığında hata vermesidir.
try
{
guncelle = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox6.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox7.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
textBox8.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
textBox9.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
textBox10.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
}
catch
{
;
}
}
private void button1_Click(object sender, EventArgs e)
{
//Kaydetme Kodları
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")
MessageBox.Show("Tüm Alanları Doldurunuz");
//TextBoxların içleri boşsa bize mesaj vericek.
else
{
//Burda daha önce aynı tc nin kayıtlı olup olmadığına bakıyoruz.
//ilk önce tc alanını çekiyo ve textbox1 deki metinle karşılaştırıyo aynıysa alltaki mesajı veriyor.
//-------------------------------------------------------------------------------------------------
string sorgu = "Select *From Tablo1 WHERE tc='" + textBox1.Text + "'";
bag.Open();
OleDbCommand okuma = new OleDbCommand(sorgu, bag);
OleDbDataReader oku = okuma.ExecuteReader();
string kontrol = "";
while (oku.Read() == true)
{
kontrol = (oku[0].ToString());
}
bag.Close();
if (textBox1.Text == kontrol)
MessageBox.Show("Bu TC Kayıtlıdır.", "Dikkat", MessageBoxButtons.OK, MessageBoxIcon.None);
//-------------------------------------------------------------------------------------------------
//Eğer aynı değilse burdaki kaydetme kodları çalışıyor.
else
{
string tc = textBox1.Text;
string ad = textBox2.Text;
string soyad = textBox3.Text;
string il = textBox4.Text;
string ilce = textBox5.Text;
bag.Open();
kmt.Connection = bag;
kmt.CommandText = "INSERT INTO Tablo1 VALUES ('" + tc + "','" + ad + "','" + soyad + "','" + il + "','" + ilce + "')";
kmt.ExecuteNonQuery();
bag.Close();
MessageBox.Show("Kayıt Tamamlandı.", "Kayıt Başarılı", MessageBoxButtons.OK, MessageBoxIcon.Information);
//Burda DataGrid' i temizliyor ve tekrar verileri çekiyor.
dataGridView1.Columns.Clear();
dtst.Tables.Clear();
dataGridView1.Refresh();
listele();//Listelemeyi tekrar yapıyor.
button2.PerformClick();//Bu kod button2 nin çalışmasını sağlar.Yani TextBoxları Temizliyor.
kayitsayisi();//Kayıt Sayısını getiriyor.
}
}
}
private void button4_Click(object sender, EventArgs e)
{
//Güncelleme Kodları
if (textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "" || textBox10.Text == "")
MessageBox.Show("Tüm Alanları Doldurunuz");
//TextBoxların içleri boşsa bize mesaj vericek.
else
{
//Burda güncellemeyi tek tek yapıyo ama tc yi değiştiremiyoruz onu bir kere girebiliyoruz.
bag.Open();
OleDbCommand komut1 = new OleDbCommand("Update Tablo1 set ad='" + textBox7.Text + "' where tc like'" + guncelle + "'", bag);
komut1.ExecuteNonQuery();
OleDbCommand komut2 = new OleDbCommand("Update Tablo1 set soyad='" + textBox8.Text + "' where tc like'" + guncelle + "'", bag);
komut2.ExecuteNonQuery();
OleDbCommand komut3 = new OleDbCommand("Update Tablo1 set il='" + textBox9.Text + "' where tc like'" + guncelle + "'", bag);
komut3.ExecuteNonQuery();
OleDbCommand komut4 = new OleDbCommand("Update Tablo1 set ilce='" + textBox10.Text + "' where tc like'" + guncelle + "'", bag);
komut4.ExecuteNonQuery();
//Burda DataGrid' i temizliyor ve tekrar verileri çekiyor.
bag.Close();
dataGridView1.Columns.Clear();
dtst.Tables.Clear();
dataGridView1.Refresh();
listele();//Listelemeyi tekrar yapıyor.
button2.PerformClick();//Bu kod button2 nin çalışmasını sağlar.Yani TextBoxları Temizliyor.
kayitsayisi();//Kayıt Sayısını getiriyor.
}
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox6.Text == "")
//Eğer Textbox6 boşsa yani sağdaki text oraya gelicek çünkü kayıt seçilmediğinde hata vercek
{
MessageBox.Show("Kayıt Seçilmedi");
}
else
{
//Kayıt seçildiğinde silinsin mi diye sorcak yanıt evetse silme kodunu çalıştırcak.
DialogResult cevap;
cevap = MessageBox.Show("Kaydı silmek istediğinizden emin misiniz?", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (cevap == DialogResult.Yes)
{
bag.Open();
kmt.Connection = bag;
kmt.CommandText = "DELETE from Tablo1 WHERE tc='" + textBox6.Text + "'";
kmt.ExecuteNonQuery();
kmt.Dispose();
bag.Close();
MessageBox.Show("Kayıt Silindi.", "Kayıt Silme", MessageBoxButtons.OK);
//Burda DataGrid' i temizliyor ve tekrar verileri çekiyor.
dataGridView1.Columns.Clear();
dtst.Tables.Clear();
dataGridView1.Refresh();
listele();//Listelemeyi tekrar yapıyor.
button2.PerformClick();//Bu kod button2 nin çalışmasını sağlar.Yani TextBoxları Temizliyor.
kayitsayisi();//Kayıt Sayısını getiriyor.
}
}
}
private void button2_Click(object sender, EventArgs e)
{
///TextBox ların içini temizliyoruz burda
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
textBox10.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
//Form açılır açılmaz datagrid üzerine verileri çekiyor ve kayıt sayısını getiriyor.
listele();
kayitsayisi();
button2.PerformClick();
}
private void button5_Click(object sender, EventArgs e)
{
//Burda adı yazılan kişinin arka plan rengi kırmızı olacak
bag.Open();
DataSet dtst = new DataSet();
string sorgu = "SELECT * FROM Tablo1";
OleDbDataAdapter adtr = new OleDbDataAdapter(sorgu, bag);
adtr.Fill(dtst, "Tablo1");
dataGridView1.DataSource = dtst.Tables[0];
for (int i = 0; i < dtst.Tables[0].Rows.Count; i++)
{
string a = Convert.ToString(dtst.Tables[0].Rows[i][1]);//ada göre burası dizi mantığına göre arıyor
//1'i 0 yaparsanız tc ye göre yapar 2 yaprsanız soyada göre
//Adı yazılan kişinin(kişilerin)arkaplanını kırmızı yapıcak.
if (a == textBox11.Text)
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
}
bag.Close();
//Kayıt Sayısını Getiriyor.
for (int i = 0; i < dataGridView1.RowCount; i++)
{
label7.Text = Convert.ToString(i);
}
}
private void button6_Click(object sender, EventArgs e)
{
//Ada göre arama yapmak için kullanılan kodlar
bag.Open();
DataSet dtst = new DataSet();
string sorgu = "SELECT * FROM Tablo1 where ad='" + textBox12.Text + "'";
//Sorguda ad' soyad yaparsanız soyada göre arama yapar ne yazarsanız ona göre arar.
//Burdada listeler.
OleDbDataAdapter adtr = new OleDbDataAdapter(sorgu, bag);
adtr.Fill(dtst, "Tablo1");
dataGridView1.DataSource = dtst.Tables[0];
bag.Close();
//Kayıt Sayısını Getiriyor.
for (int i = 0; i < dataGridView1.RowCount; i++)
{
label7.Text = Convert.ToString(i);
}
}
private void button7_Click(object sender, EventArgs e)
{
listele();
kayitsayisi();
button2.PerformClick();
}
}
}
İndirme Linki
http://turbobit.net/xxepjjsa84nx.html

Hiç yorum yok:

Yorum Gönder