
SqlConnection baglanti = new SqlConnection("Server=.;Database=CetinYazilim;Integrated Security=true;");
private void Listele()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from Personeller", baglanti);
DataTable dt = new DataTable();
adp.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns[0].Visible = false;
}
string ad = txtAdi.Text;
string soyad = txtSoyad.Text;
int yas = Convert.ToInt32(txtYas.Text);
string dogumYeri = txtDogumYeri.Text;
int maas = Convert.ToInt32(txtMaas.Text);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into Personeller (adi,soyadi,yas,dogumyeri,maas) values ('" + ad + "','" + soyad + "'," + yas + ",'" + dogumYeri + "'," + maas + ")";
cmd.Connection = baglanti;
baglanti.Open();
int etk = cmd.ExecuteNonQuery();
if (etk>0)
{
MessageBox.Show("Kayıt Eklendi");
Listele();
}
else
{
MessageBox.Show("Kayıt Eklenirken Hata Oluştu");
}
baglanti.Close();
int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
SqlCommand komut = new SqlCommand(string.Format("delete from personeller where PersonelId={0}",id),baglanti);
baglanti.Open();
int etk = komut.ExecuteNonQuery();
if (etk>0)
{
MessageBox.Show("Kayıt Silindi");
Listele();
}
else
{
MessageBox.Show("Kayıt silinirken hata oluştu");
}
int personelId = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
SqlCommand komut = new SqlCommand(string.Format("update personeller set Adi='{0}',Soyadi='{1}',Yas={2},DogumYeri='{3}',Maas={4} where PersonelId={5}",txtAdi.Text,txtSoyad.Text,txtYas.Text,txtDogumYeri.Text,txtMaas.Text,personelId),baglanti);
baglanti.Open();
int etk = komut.ExecuteNonQuery();
if (etk>0)
{
MessageBox.Show("Kayıt Güncellendi");
Listele();
}
else
{
MessageBox.Show("Güncelleme başarısız.");
}
baglanti.Close();
Ekleme silme ve güncelleme işlemlerini parametre ile yaparak sql injection’u önlemiş oluruz.