16-11-2015, 23:59
Ben proje ödevi hazırlarken bu konularla ilgili bayağı bir bilgi kirliliği ile karşılaşmıştım. Bir nebzede olsa C# da SQL ile yapacağınız işlemlerde temel düzeyde de olsa yararlı olması dileğiyle...
BAĞLANTI YAPMA
SqlConnection baganti = new SqlConnection("Data Source=abc\\SQLEXPRESS;Initial Catalog=ders;Integrated Security=True");
VERİTABANINA KAYIT EKLEME
private void button1_Click(object sender, EventArgs e)
{
baganti.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = baganti;
cmd.CommandText = "insert into ogr(OGRNODTAR,CINS) VALUES(@no,@ad,@sad,@dtar,@bolum,@cins,@adres)";
cmd.Parameters.AddWithValue("@no", textBox1.Text);
cmd.Parameters.AddWithValue("@dtar", dateTimePicker1.Value.ToShortDateString());
cmd.Parameters.AddWithValue("@bolum", comboBox1.Text);
if (radioButton1.Checked)
cmd.Parameters.AddWithValue("@cins", "ERKEK");
else
cmd.Parameters.AddWithValue("@cins", "BAYAN");
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("KAYIT EKLENDİ");
baganti.Close();
verileri_goster();
}
else
MessageBox.Show("KAYIT EKLENEMEDİ");
}
KAYIT GÜNCELLEME
SqlCommand cmd = new SqlCommand();
cmd.Connection = baganti;
string ID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
cmd.CommandText = "UPDATE OGR SET OGRNO=@noDTAR=@dtar, BOLUM=@bolum, CINS=@cins where ID=@id";
cmd.Parameters.AddWithValue("@no", textBox1.Text);
cmd.Parameters.AddWithValue("@dtar", dateTimePicker1.Value.ToShortDateString());
cmd.Parameters.AddWithValue("@bolum", comboBox1.Text);
if (radioButton1.Checked)
cmd.Parameters.AddWithValue("@cins", "ERKEK");
else
cmd.Parameters.AddWithValue("@cins", "BAYAN");
cmd.Parameters.AddWithValue("@id", ID);
baganti.Open();
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("KAYIT GUNCELLENDI");
baganti.Close();
verileri_goster();
}
}
KAYIT SİLME
SqlCommand cmd = new SqlCommand();
string ID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
cmd.Connection = baganti;
cmd.CommandText = "DELETE FROM OGR WHERE ID=" + ID;
baganti.Open();
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("KAYIT SILINDI");
baganti.Close();
verileri_goster();
}
}
DATAGRİDDEN VERİLERİ İLGİLİ ALANLARA ÇEKME
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
dateTimePicker1.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
comboBox1.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
if (dataGridView1.SelectedRows[0].Cells[6].Value.ToString() == "ERKEK")
radioButton1.Checked = true;
else
radioButton2.Checked = true;
}
}
KAYITLARI LİSTELE
void verileri_goster()
{
SqlCommand cmd = new SqlCommand("select * from OGR", baganti);
baganti.Open();
SqlDataReader oku = cmd.ExecuteReader();
DataTable tablo = new DataTable();
tablo.Load(oku);
dataGridView1.DataSource = tablo;
dataGridView1.Columns[0].Visible = false;
baganti.Close();
}
BAĞLANTI YAPMA
SqlConnection baganti = new SqlConnection("Data Source=abc\\SQLEXPRESS;Initial Catalog=ders;Integrated Security=True");
VERİTABANINA KAYIT EKLEME
private void button1_Click(object sender, EventArgs e)
{
baganti.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = baganti;
cmd.CommandText = "insert into ogr(OGRNODTAR,CINS) VALUES(@no,@ad,@sad,@dtar,@bolum,@cins,@adres)";
cmd.Parameters.AddWithValue("@no", textBox1.Text);
cmd.Parameters.AddWithValue("@dtar", dateTimePicker1.Value.ToShortDateString());
cmd.Parameters.AddWithValue("@bolum", comboBox1.Text);
if (radioButton1.Checked)
cmd.Parameters.AddWithValue("@cins", "ERKEK");
else
cmd.Parameters.AddWithValue("@cins", "BAYAN");
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("KAYIT EKLENDİ");
baganti.Close();
verileri_goster();
}
else
MessageBox.Show("KAYIT EKLENEMEDİ");
}
KAYIT GÜNCELLEME
SqlCommand cmd = new SqlCommand();
cmd.Connection = baganti;
string ID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
cmd.CommandText = "UPDATE OGR SET OGRNO=@noDTAR=@dtar, BOLUM=@bolum, CINS=@cins where ID=@id";
cmd.Parameters.AddWithValue("@no", textBox1.Text);
cmd.Parameters.AddWithValue("@dtar", dateTimePicker1.Value.ToShortDateString());
cmd.Parameters.AddWithValue("@bolum", comboBox1.Text);
if (radioButton1.Checked)
cmd.Parameters.AddWithValue("@cins", "ERKEK");
else
cmd.Parameters.AddWithValue("@cins", "BAYAN");
cmd.Parameters.AddWithValue("@id", ID);
baganti.Open();
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("KAYIT GUNCELLENDI");
baganti.Close();
verileri_goster();
}
}
KAYIT SİLME
SqlCommand cmd = new SqlCommand();
string ID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
cmd.Connection = baganti;
cmd.CommandText = "DELETE FROM OGR WHERE ID=" + ID;
baganti.Open();
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("KAYIT SILINDI");
baganti.Close();
verileri_goster();
}
}
DATAGRİDDEN VERİLERİ İLGİLİ ALANLARA ÇEKME
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
dateTimePicker1.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
comboBox1.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
if (dataGridView1.SelectedRows[0].Cells[6].Value.ToString() == "ERKEK")
radioButton1.Checked = true;
else
radioButton2.Checked = true;
}
}
KAYITLARI LİSTELE
void verileri_goster()
{
SqlCommand cmd = new SqlCommand("select * from OGR", baganti);
baganti.Open();
SqlDataReader oku = cmd.ExecuteReader();
DataTable tablo = new DataTable();
tablo.Load(oku);
dataGridView1.DataSource = tablo;
dataGridView1.Columns[0].Visible = false;
baganti.Close();
}