|
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsVeriTabaniUygulamasi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Listele()
{
using (var dt = new DataTable())
{
using (var cmd = new SqlCommand())
{
cmd.Connection = DM.Baglanti;
cmd.CommandText = "SELECT * FROM ISIMLER";
dt.Load(cmd.ExecuteReader());
}
dataGridView1.DataSource = dt.DefaultView;
}
}
private void btnListele_Click(object sender, EventArgs e)
{
this.Listele();
}
//INSERT(KAYDET)
private void btnKaydet_Click(object sender, EventArgs e)
{
using (var cmd = new SqlCommand())
{
cmd.Connection = DM.Baglanti;
cmd.CommandText = "INSERT INTO ISIMLER(AD,SOYAD) VALUES (@AD,@SOYAD)";
cmd.Parameters.AddWithValue("@AD", txtAD.Text.Trim());
cmd.Parameters.AddWithValue("@SOYAD", txtSOYAD.Text.Trim());
cmd.ExecuteNonQuery();
this.Temizle();
}
this.Listele();
}
private void Temizle()
{
foreach (Control Nesne in this.Controls)
{
if (Nesne.GetType().FullName == "System.Windows.Forms.TextBox")
Nesne.Text = string.Empty;
}
}
//DELETE(SIL)
private void btnSil_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Bak Siliyorum Ha ?", "dikkat!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
int RID = int.Parse(dataGridView1.CurrentRow.Cells["RID"].Value.ToString());
using (var cmd = new SqlCommand())
{
cmd.Connection = DM.Baglanti;
cmd.CommandText = "DELETE FROM ISIMLER WHERE RID=@RID";
cmd.Parameters.AddWithValue("@RID", RID);
cmd.ExecuteNonQuery();
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
}
}
}
private void btnDefolupGidiyorum_Click(object sender, EventArgs e)
{
DM.CloseDB();
Application.Exit();
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow != null)
{
txtAD.Focus();
txtRID.Text = dataGridView1.CurrentRow.Cells["RID"].Value.ToString();
txtAD.Text = dataGridView1.CurrentRow.Cells["AD"].Value.ToString();
txtSOYAD.Text = dataGridView1.CurrentRow.Cells["SOYAD"].Value.ToString();
}
}
//UPDATE(GÜNCELLE)
private void btnDegistir_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow != null)
{
int RID = int.Parse(dataGridView1.CurrentRow.Cells["RID"].Value.ToString());
using (var cmd = new SqlCommand())
{
cmd.Connection = DM.Baglanti;
cmd.CommandText = "UPDATE ISIMLER SET AD=@AD,SOYAD=@SOYAD WHERE RID=@RID";
cmd.Parameters.AddWithValue("@RID", RID);
cmd.Parameters.AddWithValue("@SOYAD", txtAD.Text.Trim());
cmd.Parameters.AddWithValue("@AD", txtSOYAD.Text.Trim());
cmd.ExecuteNonQuery();
dataGridView1.CurrentRow.Cells["AD"].Value = txtAD.Text.Trim();
dataGridView1.CurrentRow.Cells["SOYAD"].Value = txtSOYAD.Text.Trim();
}
}
}
//Her saniye Baglanti durumu yazdiriliyor
private void timer1_Tick(object sender, EventArgs e)
{
durumx.Text = DM.GetConnectionState();
}
}
}
|