
Merhaba arkadaşlar.
internette bir çok sayfada c# üzerinden sql bağlantısı kayıt ekleme silme ile ilgili birçok bilgi bulabilirsiniz.
fakat bu işlemleri metotlarla yapmak çin araştırma yaparsanız pek fazla kaynak yok.
ben arşivimdeki bu bilgileri sizinle paylaşacağım.
metotlarla yapacağımız bu işlemleri parça parça sizinle paylaşacağım.
SQL bağlantı Oluşturma
string bcumlesi = "Server=muhendisarsivi;User ID=sa;Password=123;Database=ogrenci;"; SqlConnection baglan; SqlCommand komut; public datab() { baglan = new SqlConnection(bcumlesi); baglan.Open(); }
Kullanıcı Girişi
datab.cs
public int kgiris(string ad, string sifre) { string sorgu = "select COUNT(*) AS say from Kullanicilar where Giris='" + ad + "' and Sifre='" + sifre + "'"; komut = new SqlCommand(sorgu, baglan); int sonuc=(int)komut.ExecuteScalar(); return sonuc; }</pre> <pre>
form sayfası
datab vt = new datab(); int sonuc=vt.kgiris(textBox1.Text,textBox2.Text); if (sonuc > 0) { // MessageBox.Show("Giriş Başarılı"); Close(); } else { MessageBox.Show("Kullanıcı Adı veya Şifre Hatalı"); return; }
Kayıt Ekleme
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; }
Kayıtları Gösterme
datab.cs
string sorgu = "Select * from Bolum"; DataSet ds=new DataSet(); SqlDataAdapter da = new SqlDataAdapter(sorgu, baglan); da.Fill(ds); return ds;
Form sayfası
datab vt = new datab(); dataGridView1.DataSource = vt.BolumGetir().Tables[0];
Kayıtları Güncelleme
datab.cs
public int ogrencikaydiguncelle(double tc, int no, string Ad, string soyad, DateTime dtarihi, int dyeri, int ilce, int bolum) { komut = new SqlCommand(); komut.CommandText = "update ogrenciler set ogrencino=@no,Ad=@ad,soyad=@soyad,dtarihi=@dtarihi,dyeri=@dyeri,ilce=@ilce,bolum=@bolu m where tc=@tc"; 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ı
double tc = 0; int no = 0; if (txttc.Text != "") tc = Convert.ToDouble(txttc.Text); if (txtno.Text != "") no = Convert.ToInt32(txtno.Text); datab vt=new datab(); if(MessageBox.Show("Güncelleme Yapmak istediğinizden emin misiniz","Güncelleme",MessageBoxButtons.YesNo)==DialogResult.Yes) { int sonuc= vt.ogrencikaydiguncelle(tc, no, txtad.Text, txtsoyad.Text,dateTimePicker1.Value, Convert.ToInt32(cbsehir.SelectedValue), Convert.ToInt32(cbilce.SelectedValue), Convert.ToInt32(cbbolum.SelectedValue)); if (sonuc >0) MessageBox.Show("Güncelleme Başarılı"); else MessageBox.Show("Güncelleme Başarısız"); }
Kayıt Silme
datab.cs
public int ogrencikaydisil(double tc) { komut = new SqlCommand(); komut.CommandText = "delete from ogrenciler where tc=@tc"; komut.Parameters.Add("@tc", SqlDbType.BigInt).Value = tc; komut.Connection = baglan; int sonuc = (int)komut.ExecuteNonQuery(); return sonuc; }
Form sayfası
double tc = 0; if (txttc.Text != "") tc = Convert.ToDouble(txttc.Text); datab vt = new datab(); if (MessageBox.Show("Silmek istediğinizden emin misiniz", "Silme İşlemi", MessageBoxButtons.YesNo) == DialogResult.Yes) { int sonuc = vt.ogrencikaydisil(tc); if (sonuc > 0) MessageBox.Show("Silme İşlemi Başarılı"); else MessageBox.Show("Silme İşlemi Başarısız"); }
Bu makalemiz şimdilik bukadar.
takıldığınız bir yer olursa info@muhendisarsivi.com’a mail atabilir yada aşağıya yorum yapabilirsiniz.