buda 2. derste anlattığım konular . kodlar işine yarar veritabanına bağlanmak için

4 label , 3 textbox salla bölüm için combobox salla.üç tane tane buton koy

Butonlar : yeni ,kaydet,ara



Using system.data.sqlclient // namespacesini ekle

Sqlconnection con=new sqlconnection(“Sata source=ANAMAKİNA1;initial catalog=okul;integrated security=true”); //nesnesini oluştur boş bi yere



Formun loadına ekle form1 olana // comboba,oxın içerisini doldurur…



Try{

Con.open();

Sqlcommand cmd =new sqlcommand(“select badi from bolum order by badi”,con);

Sqldatareader dr =cmd.executereader();

While (dr.read())

Combobox1.items.add(dr.getvaule(0).tostring());

Dr.close();

}

Catch(exception ee)

{

Messagebox.show(ee.message);

}

Finally

{

Con.close();

}



Labellara : no,adı,soyadı,bölümü

Comboboxun dropdonstyle özelliğini lit yap ki comboboxa başka isim giremesin



Buton 1 yeni yeniye basılınca textboxlar temizlensin ve yeni bir bilgi kaydı yapılsın.



Textbox1 click



Textbox1.clear();

Textbox2.clear();

Textbox2.clear();

Comboboz.selextedindex=0;

Textbox1.focus(); /////////imleç textbox1 e konumlandı





Buton 2 click (kaydet)



Try

{

Con.open();

String sorgu=string.format(“select bno from bolum where badi=’{0}’”,combobox1.text);

Sqlcommand cmd=new sqlcommand(sorgu,con);

İnt sonuc =int.parse ( cmd.executescalar().tostring());

Sorgu=string.format(“insert into ogrenci values(‘{0}’,’{1}’,’{2}’,’{3}’ )”,textbox1.text,textbox2.text,textbox3.text,sonuc );

Cmd commandtext=sorgu;

Cmd.executenonquery();

Messagebox.show(“kayıt başarılı”);

}

Catch (exception ee)

{

Messagebox.show(ee.message);

}

Finally

{

Con.close();

}



Buton 3 ara. Textboxları sil ve textbox1 e konumlan



Button1_click(sender,e); //// tekrar tekrar silip imleç konumlandırmaya gerek kalmadan bu şekilde ayarladık.

İlk başta kayıt varsa var yok sa yok diyecek



Textbox1.keypress özelliğinin içerisine yaz



İf(e.keychar==13) eğer kullanıcı enter tuşuna basmışsa

{

Try

{

String sorgu=string.format(“select ogradi,ogrsadi,badi from ogrenci,bolum where ogrenci.bno=bolum.bno andogrno=’{0}’”,textbox1.text);

Con.open()=;

Sqlcomamnd cmd=new sqlcommand(sorgu,con);

Sqldatareader dr=cmd.executereader();

İf(dr.hasrows==true) /////db boş değilse

{

Dr.read();

Textbox2.text=dr.getvalue(0).tostring();

Textbox3.text=dr.getvalue(1).tostring();

Combobox1.text=dr.getvalue(2).tostring();

Dr.close();

}

Else

Messagebox.show(“Kayıt Bulunamadı”);

}

Catch(exeption ee)

{

Messagebox.show(ee.message);

}

Finally

{

Con.close();

}

}



Buton 4 clik değiştirme



String sorgu= string.format(“update ogrenci set ogradi=’{0}’,ogrsadi=’{1}’,bno={2} where ogrno=’{3}’ ”,textbox2.text,textbox3.text,bno,textbox1.te xt);

////buda silmek

Sorgu= string.format(“delete from ogrenci where ogrno=’{0}’”,textbox1.text)