SQL Veritabanına bağlantı örneği için tıklayınız….
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string baglanti = “Data Source=.\\SQLEXPRESS; Initial Catalog=test; Integrated Security=true;”;
SqlConnection bagla = new SqlConnection(baglanti);
if (bagla.State==ConnectionState.Closed)
bagla.Open();
SqlCommand komut = new SqlCommand(“insert into urun(id,tur) values (1,’Giyecek’)”, bagla);
komut.ExecuteNonQuery();
MessageBox.Show(“Ekleme işlemi başarılı…”);
}
private void button2_Click(object sender, EventArgs e)
{
string baglanti = “Data Source=.\\SQLEXPRESS; Initial Catalog=test; Integrated Security=true;”;
SqlConnection bagla = new SqlConnection(baglanti);
if (bagla.State == ConnectionState.Closed)
bagla.Open();
SqlCommand komut = new SqlCommand(“update urun set tur=’Yiyecek’ where id=1”, bagla);
komut.ExecuteNonQuery();
MessageBox.Show(“GÜncelleme işlemi başarılı…”);
}
private void button3_Click(object sender, EventArgs e)
{
string baglanti = “Data Source=.\\SQLEXPRESS; Initial Catalog=test; Integrated Security=true;”;
SqlConnection bagla = new SqlConnection(baglanti);
if (bagla.State == ConnectionState.Closed)
bagla.Open();
SqlCommand komut = new SqlCommand(“delete from urun where id=1”, bagla);
komut.ExecuteNonQuery();
MessageBox.Show(“Silme işlemi başarılı…”);
}
private void button4_Click(object sender, EventArgs e)
{
string baglanti = “Data Source=.\\SQLEXPRESS; Initial Catalog=test; Integrated Security=true;”;
SqlConnection bagla = new SqlConnection(baglanti);
if (bagla.State == ConnectionState.Closed)
bagla.Open();
SqlCommand komut = new SqlCommand(“select * from urun”, bagla);
SqlDataReader oku = komut.ExecuteReader();
DataTable tablo = new DataTable();
tablo.Load(oku);
dataGridView1.DataSource = tablo;
}
}
}