Ana içeriğe atla

INNER JOIN ile id ye göre Güncelleme işlemleri


 INNER JOIN ile id ye göre Güncelleme işlemleri :

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace Delete_Pro

{

    public partial class Guncelle : Form

    {

        public Guncelle()

        {

            InitializeComponent();

        }

        SqlConnection connection = new SqlConnection(@"Data Source=.;Initial Catalog=VT_Diagram;Integrated Security=True");

        DataTable dt = new DataTable();

        void Guncelleme_Bilgileri()

        {

            try

            {

                connection.Open();

                int isimID = Convert.ToInt32(lbl_ID.Text); // Güncelleme işlemi için ID değeri


                // Soyisim Güncelleme

                string guncelle_Soyisim = "UPDATE Soyisim SET soyisim=@soyisim WHERE isim_Id=@isim_Id";

                SqlCommand cmd = new SqlCommand(guncelle_Soyisim, connection);

                cmd.Parameters.AddWithValue("@soyisim", txt_soyisim.Text);

                cmd.Parameters.AddWithValue("@isim_Id", isimID);

                cmd.ExecuteNonQuery();


                // İsim Güncelleme

                string guncelle_isim = "UPDATE isim SET isim=@isim WHERE isim_Id=@isim_Id";

                SqlCommand cmd_isim = new SqlCommand(guncelle_isim, connection);

                cmd_isim.Parameters.AddWithValue("@isim", txt_Isim.Text);

                cmd_isim.Parameters.AddWithValue("@isim_Id", isimID);

                cmd_isim.ExecuteNonQuery();


                MessageBox.Show("Güncelleme başarılı!");

                Verileri_Listele(); // Güncel verileri tekrar yükle

            }

            catch (Exception ex)

            {

                MessageBox.Show("Hata \n" + ex.Message);

            }

        }


        void Verileri_Listele()

        {

            //Veri Tabanından Verileri Getirme

            try

            {

                string sql = @"SELECT i.isim_Id, i.isim, s.soyisim

                       FROM isim i

                       INNER JOIN soyisim s ON i.isim_Id = s.isim_Id";


                SqlCommand cmd = new SqlCommand(sql, connection);

                SqlDataAdapter da = new SqlDataAdapter(cmd);

                DataTable dt = new DataTable();

                da.Fill(dt);

                dataGrid_Bilgiler.DataSource = dt;

            }

            catch (Exception ex)

            {

                MessageBox.Show("Hata: \n" + ex.Message);

            }

        }

        private void Guncelle_Load(object sender, EventArgs e)

        {

            Verileri_Listele();   

        }


        private void btn_Guncelle_Click(object sender, EventArgs e)

        {

            try

            {                

                Guncelleme_Bilgileri();

            }

            catch (Exception ex)

            {


                MessageBox.Show("Guncelleme Esnasında bir problem oldu " + ex.Message);

                return;

            }

            finally 

            {

                connection.Close();

            }

        }


        private void dataGrid_Bilgiler_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {

            //DataGrid seçilen verilerin text 'lere aktarılması

            txt_Isim.Text = dataGrid_Bilgiler.CurrentRow.Cells["isim"].Value.ToString();

            txt_soyisim.Text = dataGrid_Bilgiler.CurrentRow.Cells["soyisim"].Value.ToString();

            //id ye göre işlemlerin gerçekleşmesi

            lbl_ID.Text = Convert.ToString(dataGrid_Bilgiler.CurrentRow.Cells["isim_Id"].Value);

            int isim_Id = Convert.ToInt32(lbl_ID.Text);

        }

    }

}




Bu blogdaki popüler yayınlar

FOREGIN KEY ve INNER JOIN ile ilişkili tablo oluşturma Örneği

--1)DataBase oluşturma Sorgusu --CREATE DATABASE Join_Alistirma; --GO --USE Join_Alistirma; --GO --2)musteri Tablosu oluşturma --CREATE TABLE musteri ( --    musteri_ID INT IDENTITY(1,1) PRIMARY KEY, --    adi_soyadi NVARCHAR(100) NOT NULL, --    telefon NVARCHAR(20) NOT NULL --); ---3)araclar Tablosunu Oluştur (FOREIGN KEY ile) --CREATE TABLE araclar ( --    id INT IDENTITY(1,1) PRIMARY KEY, --    musteri_ID INT NOT NULL, --    plaka NVARCHAR(20) NOT NULL, --    marka NVARCHAR(50), --    model NVARCHAR(50), --    yil INT, --    FOREIGN KEY (musteri_ID) REFERENCES musteri(musteri_ID) --); ----------------------------------------------------------------------------- ----NOT  --musteri_ID → musteri tablosuyla bağlantılı olacak. --plaka, marka, model, yil alanları araç bilgilerini tutacak. ----------------------------------------------------------------------------- ---4)Tablolara muste...

Private Void Metod

 namespace Metotlar {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         //Parametre Almayan Değer Döndürmeyen Metot         //Değer döndürmeyen metot tanımlarken Başa Void yazılır         //Tekrar tekrar aynı kod parçasını yazmaya gerek kalmaz Metot adını istediğinde çağarabilirsin         //void : private void olan metot başka yerden erişelemez         //public void olanlar ise erişime açık (dışarıdan görülebilen) anlamına gelir         //Örnek : MesayYaz Metotu: Çağrıldığı yerde ekrana bir mesaj veren metot yazalım         void MesajYaz()          {             MessageBox.Show("Merhaba Ben Parametresiz...

İD ye göre işlemler C# ve Class örnekleri ile Birlikte

***************************************************************************** ✅ DbHelper.cs adında bir sınıf ile Insert , Update , Delete , GetId , SelectAll işlemlerini kolayca yapabilirsiniz. ✅ SCOPE_IDENTITY() kullanarak son eklenen kaydın ID’sini çekme örneği var. ✅ Bu ID’yi Label veya TextBox ’a yazdırabilir, Update veya Delete için tekrar kullanabilirsiniz. ✅ DataGridView ile tüm verileri listeleme örneği de dahil. *****************************************************************************  // ✅ DbHelper.cs using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; public static class DbHelper {     private static string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=YourDatabase;Integrated Security=True";     public static object InsertAndGetId(string query, Dictionary<string, string> parameters)     {         using (SqlConnection connection = new SqlConne...