
Merhaba arkadaşlar bu makalemizde metodlarla c# üzerinden Sql’e kayıt ekleyeceğiz.
datab.cs
public int ogrencikaydet(double tc,int no,string Ad,string soyad,DateTime dtarihi,int dyeri,int ilce,int bolum) { komut = new SqlCommand(); komut.CommandText = "insert into ogrenciler(tc,ogrencino,Ad,soyad,dtarihi,dyeri,ilce,bolum) values (@tc,@no,@Ad,@soyad,@dtarihi,@dyeri,@ilce,@bolum)"; komut.Parameters.Add("@tc", SqlDbType.BigInt).Value = tc; komut.Parameters.Add("@no", SqlDbType.Int).Value = no; komut.Parameters.Add("@ad", SqlDbType.VarChar).Value = Ad; komut.Parameters.Add("@soyad", SqlDbType.VarChar).Value = soyad; komut.Parameters.Add("@dtarihi", SqlDbType.DateTime).Value = dtarihi; komut.Parameters.Add("@dyeri", SqlDbType.Int).Value = dyeri; komut.Parameters.Add("@ilce", SqlDbType.Int).Value = ilce; komut.Parameters.Add("@bolum", SqlDbType.Int).Value = bolum; komut.Connection = baglan; int sonuc = (int)komut.ExecuteNonQuery(); return sonuc; }
form sayfası
datab vt = new datab(); int sonuc = vt.ogrencikaydet(Convert.ToDouble(txttc.Text), Convert.ToInt32(txtno.Text), txtad.Text, txtsoyad.Text, dateTimePicker1.Value, Convert.ToInt32(cbsehir.SelectedValue), Convert.ToInt32(cbilce.SelectedValue), Convert.ToInt32(cbbolum.SelectedValue)); if (sonuc > 0) { MessageBox.Show("Kayıt Başarılı"); } else { MessageBox.Show("Kayıt Başarısız"); return; }