SQL Ekleme Silme Güncelleme Arama
Bağlantı
Kütüphane Ekleme:
using System.Data.SqlClient;
Bağlanrı Adresi:
SqlConnection baglan = new SqlConnection("Data Source=desktop-ikf7f4q\SQLEXPRESS;Initial Catalog=Kutuphane;Integrated Security=True");
Göster
private void verilerigoster(string veriler) { SqlDataAdapter da = new SqlDataAdapter(veriler, baglanti); DataSet ds = new DataSet();//data set oluşturduk da.Fill(ds);//data set dolduruluyor dataGridView1.DataSource = ds.Tables[0]; } private void button1_Click(object sender, EventArgs e) { verilerigoster("Select * from kitaplar"); }
Ekle
private void button2_Click(object sender, EventArgs e) { baglanti.Open(); SqlCommand komut = new SqlCommand("INSERT INTO kitaplar (ad,yazar,sayfano,yayinevi,tur) VALUES (@adi,@yazari,@sayfanosu,@basimevi,@turu)", baglanti); komut.Parameters.AddWithValue("@adi",textBox1.Text); komut.Parameters.AddWithValue("@yazari", textBox2.Text); komut.Parameters.AddWithValue("@sayfanosu", textBox3.Text); komut.Parameters.AddWithValue("@basimevi", textBox4.Text); komut.Parameters.AddWithValue("@turu", textBox5.Text); komut.ExecuteNonQuery(); verilerigoster("Select * from kitaplar"); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); textBox5.Clear(); textBox1.Focus(); }
Silme
private void button3_Click(object sender, EventArgs e) { baglanti.Open(); SqlCommand komut = new SqlCommand("delete from kitaplar where ad=@adi", baglanti); komut.Parameters.AddWithValue("@adi", textBox6.Text); komut.ExecuteNonQuery(); verilerigoster("Select * from kitaplar"); baglanti.Close(); textBox6.Clear(); }
Arama
private void button4_Click(object sender, EventArgs e) { baglanti.Open(); SqlCommand komut = new SqlCommand("Select * from kitaplar where ad like '%"+ textBox7.Text +"%'", baglanti); SqlDataAdapter da = new SqlDataAdapter(komut); DataSet ds = new DataSet(); da.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; baglanti.Close(); }
Data Grid View'de Cell Click İşlemi
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { int secilialan = dataGridView1.SelectedCells[0].RowIndex; string ad = dataGridView1.Rows[secilialan].Cells[1].Value.ToString(); string yazar = dataGridView1.Rows[secilialan].Cells[2].Value.ToString(); string sayfano = dataGridView1.Rows[secilialan].Cells[3].Value.ToString(); string yayinevi = dataGridView1.Rows[secilialan].Cells[4].Value.ToString(); string tur = dataGridView1.Rows[secilialan].Cells[5].Value.ToString(); textBox1.Text = ad; textBox2.Text = yazar; textBox3.Text = sayfano; textBox4.Text = yayinevi; textBox5.Text = tur; }
Güncelleme
private void button5_Click(object sender, EventArgs e) { baglanti.Open(); SqlCommand komut = new SqlCommand(" update kitaplar set yazar='" + textBox2.Text + "',sayfano='" + textBox3.Text + "',yayinevi='" + textBox4.Text + "',tur='" + textBox5.Text + "' where ad='"+ textBox1.Text + "'", baglanti); komut.ExecuteNonQuery(); verilerigoster("Select * from kitaplar"); baglanti.Close(); }