0

Ekleme -Silme -Güncelleme-Listeleme Örneği

Ekleme -Silme -Güncelleme-Listeleme Örneği için tıklayınız.

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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int deger;
OleDbConnection conn = new OleDbConnection
(“Provider=Microsoft.Ace.Oledb.12.0;Data Source= C:\\Users\\PC\\Documents\\visual studio 2013\\Projects\\WindowsFormsApplication2\\WindowsFormsApplication2\\deneme.accdb ;Persist Security Info=True”);
private void button1_Click(object sender, EventArgs e)
{
conn.Open();
//bağlantı açılıyor….
string sorgu = “Select * from kimlik”;//kimlik tablosundaki bütün verileri getir…
OleDbDataAdapter adp = new OleDbDataAdapter(sorgu, conn);
//veriyi getirecek adp nesnesi oluşturuluyor….
DataSet ds = new DataSet();
adp.Fill(ds);
//Gelen veri datasete aktaralıyor…
dataGridView1.DataSource = ds.Tables[0];
//gridviewde bilgiler listeleniyor…
conn.Close();
//bağlantı kapatılıyor…
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
deger = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
conn.Open();
string sorgu = “update kimlik set Ad='” + textBox1.Text + “‘,Soyad='” + textBox2.Text + “‘,DoğumYer='” + textBox3.Text + “‘ where Tc_No=” + deger + “”;
OleDbCommand cmd = new OleDbCommand(sorgu, conn);
cmd.ExecuteNonQuery();
MessageBox.Show(“Veri Güncellendi..”);
conn.Close();
temizle();
}

private void button3_Click(object sender, EventArgs e)
{
conn.Open();
string sorgu = “delete from kimlik where Tc_No=” + deger + “”;
OleDbCommand cmd = new OleDbCommand(sorgu, conn);
cmd.ExecuteNonQuery();
MessageBox.Show(“Veri Silindi..”);
conn.Close();
temizle();
}

private void button4_Click(object sender, EventArgs e)
{
conn.Open();
string sorgu = “insert into kimlik (Ad,Soyad,DoğumYer) values (@adi,@soyadi,@dyeri)”;
OleDbCommand cmd = new OleDbCommand(sorgu, conn);
cmd.Parameters.AddWithValue(“@adi”, textBox1.Text);
cmd.Parameters.AddWithValue(“@soyadi”, textBox2.Text);
cmd.Parameters.AddWithValue(“@dyeri”, textBox3.Text);
cmd.ExecuteNonQuery();// cmd nesnesini çalıştır
MessageBox.Show(“Veri Eklendi..”);
conn.Close();
temizle();
}
private void temizle()
{
foreach (Control nesne in this.Controls)
{
if (nesne is TextBox)
{
TextBox textbox = (TextBox)nesne;
textbox.Clear();
}
}
}
}
}

admin

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir