Son Yazılar

C# Sql Görüntüleme, Güncelleme , Ekleme ve Silme Komutları

C# Sql Görüntüle Güncelle Ekle Sil Komutları
 


Visual Studio 2017 ile C Sharp ta Kullanıcı Formu ile SQL Server da veri güncelleme, görüntüleme, ekleme ve silme komutlarını anlatacağız.  Visual Studio da bir C# formu oluşturup içene Verileri işlem yapacağımız sırayı belirleyen bir Combobox ekleyelim. Combobox seçimine göre SQL tablosundaki yapacağımız işleme göre veriler Textbox lara aktarılacak.Daha sonra Sql Tablo Sütun sayısı kadar Textbox ekleyelim. Biz Ad, Soyad ve Parola şeklinde üç sütunu da işlem yapacağımız için 3 tane Textbox ekleyeceğiz ve tüm bu işlemleri gerçekleştirecek olan bir butonları ekleyelim. Bunları yaptıktan sonra SQL server da bir tablo oluşturup SıraNo adındaki sütunu Primary Key olarak belirleyelim. Form alanında iken komut butonuna çift tıklayarak açılan ekran da aşağıdaki kodları form kod alanına yazalım.



C# SQL Görüntüle Güncelle Ekle Sil Tablo Tasarımı




C# SQL Görüntüle Güncelle Ekle Sil Tablo




C# SQL Görüntüle Güncelle Ekle Sil Form




C# Uygulama Kodu

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.Runtime.InteropServices;
using System.Data.SqlClient;

namespace SqlKodlarıUpdate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlConnection sqlBaglantisi = new SqlConnection(@"Data Source=…….\sqlexpress;Initial Catalog=C Sql Update;Integrated Security=True");

        [DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
        private extern static void ReleaseCapture();
        [DllImport("user32.DLL", EntryPoint = "SendMessage")]
        private extern static void SendMessage(System.IntPtr hwnd, int wmsg, int wparam, int lparam);

        private void pnlKontrol_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, 0x112, 0xf012, 0);
        }

        private void btnKapat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnKüçült_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void btnGüncelle_Click(object sender, EventArgs e)
        {
            sqlBaglantisi.Open();
            SqlDataAdapter sqladaptör = new SqlDataAdapter("select count(*) from C_Sql_Update where SıraNo = '" + cmBoxSıra.Text + "'", sqlBaglantisi);
            DataTable dtablo = new DataTable();
            sqladaptör.Fill(dtablo);
            
            if (dtablo.Rows[0][0].ToString() == "1")
            {
                SqlCommand sqladaptörG = new SqlCommand("update C_Sql_Update set Ad = '" + txtAd.Text.ToString() + "' ,Soyad ='" + txtSoyad.Text.ToString() +"',Parola ='" + txtParola.Text.ToString() + "' where SıraNo ='" + cmBoxSıra.Text +"'", sqlBaglantisi);
                sqladaptörG.ExecuteNonQuery();

                this.Hide();
                new Form1().Show();
            }
            else
            {
               
            }
            sqlBaglantisi.Close();
        }

        private void btnGörüntüle_Click(object sender, EventArgs e)
        {
            sqlBaglantisi.Open();
            SqlCommand verioku = new SqlCommand("select * from C_Sql_Update where SıraNo = '" + cmBoxSıra.Text.ToString() + "'", sqlBaglantisi);
            verioku.ExecuteNonQuery();
            SqlDataReader oku;
            oku = verioku.ExecuteReader();

            while (oku.Read())
            {

                txtAd.Text = oku["Ad"].ToString();
                txtSoyad.Text = oku["Soyad"].ToString();
                txtParola.Text = oku["Parola"].ToString();

            }
            oku.Close();
            sqlBaglantisi.Close();
        }

        private void btnEkle_Click(object sender, EventArgs e)
        {
            if (sqlBaglantisi.State == ConnectionState.Closed)
            {
                sqlBaglantisi.Open();
                SqlCommand veriekle = new SqlCommand("insert into C_Sql_Update (SıraNo,Ad,Soyad,Parola) values (@SıraNo,@Ad,@Soyad,@Parola)", sqlBaglantisi);
                veriekle.Parameters.AddWithValue("@SıraNo", cmBoxSıra.Text);
                veriekle.Parameters.AddWithValue("@Ad", txtAd.Text);
                veriekle.Parameters.AddWithValue("@Soyad", txtSoyad.Text);
                veriekle.Parameters.AddWithValue("@Parola", txtParola.Text);
                veriekle.ExecuteNonQuery();
                sqlBaglantisi.Close();

                this.Hide();
                new Form1().Show();
            }

            else
            {
                MessageBox.Show("Veri Kaydı Başarısız !");
            }
        }

        private void btnSil_Click(object sender, EventArgs e)
        {
            sqlBaglantisi.Open();
            SqlCommand verisil = new SqlCommand("delete from C_Sql_Update where SıraNo = '" + cmBoxSıra.Text + "'", sqlBaglantisi);
            verisil.ExecuteNonQuery();
            sqlBaglantisi.Close();
            this.Hide();
            new Form1().Show();
        }

        private void cmBoxSıra_Click(object sender, EventArgs e)
        {
            cmBoxSıra.Items.Clear();
            sqlBaglantisi.Open();
            SqlCommand verioku = new SqlCommand("select * from C_Sql_Update", sqlBaglantisi);
            SqlDataReader getir;
            getir = verioku.ExecuteReader();

            while (getir.Read())
            {
                cmBoxSıra.Items.Add(getir["SıraNo"].ToString());
            }
            getir.Close();
            sqlBaglantisi.Close();
        }
    }
}

Yorum Yayınla

Yazı Hakkındaki Düşünceleriniz...

Daha yeni Daha eski