Last active
November 20, 2018 08:43
-
-
Save canokay/1d71e7d4c8c71b9df268d245cdfb507a to your computer and use it in GitHub Desktop.
C# Database Create, update and Delete Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
using System.Data.OleDb; | |
namespace Ders2Uygulama1 | |
{ | |
public partial class Form1 : Form | |
{ | |
OleDbConnection baglanti; | |
OleDbDataAdapter da; | |
OleDbCommandBuilder cb; | |
DataTable dt = new DataTable(); | |
BindingSource bs = new BindingSource(); | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
string baglan = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=veritabani.mdb"; | |
baglanti = new OleDbConnection(baglan); | |
da = new OleDbDataAdapter("Select * from ogr", baglan); | |
cb = new OleDbCommandBuilder(da); | |
da.Fill(dt); | |
bs.DataSource = dt; | |
dataGridView1.DataSource = bs; | |
textBox1.DataBindings.Add("Text", bs, "kimlik"); | |
textBox2.DataBindings.Add("Text", bs, "adi"); | |
textBox3.DataBindings.Add("Text", bs, "soyadi"); | |
textBox4.DataBindings.Add("Text", bs, "baba_adi"); | |
textBox5.DataBindings.Add("Text", bs, "anne_adi"); | |
dataGridView1.ReadOnly = true; | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
bs.AddNew(); | |
} | |
private void button2_Click(object sender, EventArgs e) | |
{ | |
bs.EndEdit(); //BindingSource da degisiklikleri tamamlama | |
DataTable dtdegisim = new DataTable(); //datatable tanımı | |
dtdegisim = dt.GetChanges(); //db de yapilan degisikleri dtdegisime yollama | |
try | |
{ | |
da.Update(dtdegisim); | |
} | |
catch (Exception hata) | |
{ | |
MessageBox.Show(hata.Message); | |
} | |
} | |
private void button3_Click(object sender, EventArgs e) | |
{ | |
bs.CancelEdit(); | |
} | |
private void button4_Click(object sender, EventArgs e) | |
{ | |
bs.MoveFirst(); | |
} | |
private void button5_Click(object sender, EventArgs e) | |
{ | |
bs.MovePrevious(); | |
} | |
private void button6_Click(object sender, EventArgs e) | |
{ | |
bs.MoveNext(); | |
} | |
private void button7_Click(object sender, EventArgs e) | |
{ | |
bs.MoveLast(); | |
} | |
private void button8_Click(object sender, EventArgs e) | |
{ | |
baglanti.Open(); | |
OleDbCommand kaydet = new OleDbCommand("Insert into ogr () values () ",baglanti); | |
kaydet.ExecuteNonQuery(); | |
baglanti.Close(); | |
MessageBox.Show("Kayıt eklendi."); | |
} | |
private void button9_Click(object sender, EventArgs e) | |
{ | |
baglanti.Open(); | |
OleDbCommand guncelle = new OleDbCommand("update ogr () values () ", baglanti); | |
guncelle.ExecuteNonQuery(); | |
baglanti.Close(); | |
MessageBox.Show("Kayıt Güncellendi."); | |
} | |
private void button10_Click(object sender, EventArgs e) | |
{ | |
if (MessageBox.Show("Kayıdı silmek istediğinize emin misiniz?","Kayit silme",MessageBoxButtons.YesNo)==DialogResult.Yes) | |
{ | |
bs.RemoveCurrent(); | |
baglanti.Open(); | |
string sql = "Delete from ogr where kimlik=" + Int32.Parse(textBox1.Text); | |
OleDbCommand silme = new OleDbCommand(sql,baglanti); | |
silme.ExecuteNonQuery(); | |
baglanti.Close(); | |
MessageBox.Show("Kayıt silindi."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment