aşağıdaki gibi bir form oluşturalım.
kayıt ekleme yapmış olduğumuz dersteki gibi comboBox nesnesine sınıfları ekleyelim. aşağıdaki kodları gerekli nesnelerin olaylarına yazalım.
private void button3_Click(object sender, EventArgs e)
{
//Veritabanın gağlantı türü ve hangi dosyaya bağlanacağını belirtir. access dosyasını bin/debug içine attık.
OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=ogrenciler.accdb");
OleDbCommand sorgu = new OleDbCommand();//sorgu komut yapısı oluşturuldu
baglanti.Open();// bağlantı açıldı
sorgu.Connection = baglanti;
sorgu.CommandText = String.Format("select * from ogrenci_bilgi where numara={0}", textBox1.Text);
OleDbDataReader oku = sorgu.ExecuteReader();
bool durum = oku.Read();
if (durum == false)
{
MessageBox.Show("Kayıt yok");
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
comboBox1.Text = null;
textBox1.Focus();
}
else
{
textBox2.Text = oku[1].ToString();
textBox3.Text = oku[2].ToString();
comboBox1.SelectedText = oku[3].ToString();
}
baglanti.Close();//bağlantı sonlandırma.
}
private void button2_Click(object sender, EventArgs e)
{
//Veritabanın gağlantı türü ve hangi dosyaya bağlanacağını belirtir. access dosyasını bin/debug içine attık.
OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=ogrenciler.accdb");
OleDbCommand sorgu = new OleDbCommand();//sorgu komut yapısı oluşturuldu
baglanti.Open();// bağlantı açıldı
sorgu.Connection = baglanti;
sorgu.CommandText = String.Format("update ogrenci_bilgi set adi='{0}',soyadi='{1}',sinif='{2}' where numara={3}", textBox2.Text,textBox3.Text,comboBox1.Text,textBox1.Text);
int adet = sorgu.ExecuteNonQuery();
if (adet > 0)
MessageBox.Show("Kayıt Güncellendi");
else
MessageBox.Show("Kaıt güncellenmedi");
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
Yorum ekle