Ana içeriğe atla

C# Update

 

//using System.Data.SqlClient;

//Yeni Güncelleme geldi

//Araçlar>NuGet Paket Yöneticisi>Paket yöneticisi Konsole

//Install-Package Microsoft.Data.SqlClient

//Kodu Yapıştırdık

//using System.Data.SqlClient;  using Microsoft.Data.SqlClient; şeklinde değiştir ve Sorun Çözüldü

//Problem SqlConnection bağlantısının altı ve üzeri çizili idi yeni version 9 geldiği için yukarıdaki değişiklikler yapıldı


using Microsoft.Data.SqlClient;

namespace Update

{

    public partial class Form1 : Form

    {



        SqlConnection sqlBaglanti = new SqlConnection(@"Data Source=DESKTOP-0B1GIRL\SQLEXPRESS;Initial Catalog=TestVT;Integrated Security=True;TrustServerCertificate=True");     

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            // TextBox'ların boş olup olmadığını kontrol et

            /*             

            Eğer textBox'lar boşsa, sorgu çalıştırılmadan önce bu durumu kontrol ederek kullanıcıya bilgi verebilir ve gereksiz hata mesajlarının önüne geçilebilir.

            catch bloğu yalnızca SQL bağlantısı ya da sorgu ile ilgili hataları ele almalı.

            Boş alan kontrolünü try bloğuna girmeden önce yapılmalı             

             */

            if (string.IsNullOrWhiteSpace(textBox1.Text) ||

                string.IsNullOrWhiteSpace(textBox2.Text) ||

                string.IsNullOrWhiteSpace(textBox3.Text) ||

                string.IsNullOrWhiteSpace(textBox4.Text))

            {

                MessageBox.Show("Lütfen tüm alanları doldurun!");

                return; // Eğer bir alan boşsa, işlemi durdur.

            }

            try

            {

                // Bağlantıyı aç

                sqlBaglanti.Open();

                // UPDATE sorgusunu oluştur

                string updateQuery = "UPDATE Personel SET Ad = @p1, Soyad = @p2, Adres = @p3 WHERE PersonelNo = @p4";

                // SqlCommand nesnesi oluştur

                SqlCommand sqlCommand = new SqlCommand(updateQuery, sqlBaglanti);               

                // Parametreleri ekle (Değerleri TextBox'lardan alıyoruz)

                sqlCommand.Parameters.AddWithValue("@p1", textBox2.Text.Trim()); // Ad

                sqlCommand.Parameters.AddWithValue("@p2", textBox3.Text.Trim()); // Soyad

                sqlCommand.Parameters.AddWithValue("@p3", textBox4.Text.Trim()); // Adres

                sqlCommand.Parameters.AddWithValue("@p4", textBox1.Text.Trim()); // Güncellenecek PersonelNo


                // Sorguyu çalıştır

                int etkilenenSatirlar = sqlCommand.ExecuteNonQuery();


                // Sonuç mesajı

                if (etkilenenSatirlar > 0)

                {

                    MessageBox.Show("Kayıt başarıyla güncellendi!");

                }

                else

                {

                    MessageBox.Show("Hiçbir kayıt güncellenmedi.");

                }

                //Text Boxların Temizlenmesi

                textBox1.Clear();

                textBox2.Clear();

                textBox3.Clear();

                textBox4.Clear();

            }

            catch (Exception ex)

            {

                // Hata mesajı

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

            }

            finally

            {

                // Bağlantıyı kapat

                if (sqlBaglanti.State == System.Data.ConnectionState.Open)

                {

                    sqlBaglanti.Close();

                }

            }

        }

    }

}








Yorumlar

Bu blogdaki popüler yayınlar

C# Delete

//using System.Data.SqlClient; //Yeni Güncelleme geldi //Araçlar>NuGet Paket Yöneticisi>Paket yöneticisi Konsole //Install-Package Microsoft.Data.SqlClient //Kodu Yapıştırdık //using System.Data.SqlClient;  Microsoft.Data.SqlClient şeklinde değiştir ve Sorun Çözüldü //Problem SqlConnection bağlantısının altı ve üzeri çizili idi yeni version 9 geldiği için yukarıdaki değişiklikler yapıldı using Microsoft.Data.SqlClient; namespace Update {     public partial class Form1 : Form     {         SqlConnection sqlBaglanti = new SqlConnection(@"Data Source=DESKTOP-0B1GIRL\SQLEXPRESS;Initial Catalog=TestVT;Integrated Security=True;TrustServerCertificate=True");         public Form1()         {             InitializeComponent();         }         //Güncelleme İşlemi         private void button1_Click(object sender, Even...

C# Forms ve ADO.NET ile SQL Server 2022'de SQLConnection bağlantısı ile INSERT İşlemi

// SQL bağlantı yolu için sistem paketinin yüklenmesi using System; using System.Data.SqlClient; using System.Windows.Forms; namespace VeriTabanıTestProjesi {     public partial class Form1 : Form     {         // SQL bağlantı nesnesinin tanımlanması         SqlConnection sqlBaglanti = new SqlConnection(@"Data Source=DESKTOP-0B1GIRL\SQLEXPRESS;Initial Catalog=TestVT;Integrated Security=True");         public Form1()         {             InitializeComponent();         }         private void button1_Click(object sender, EventArgs e)         {             // Veri Tabanında oluşabilecek hataların yakalanması             try             {                 // SQL bağlantısının a...

Microsoft.Data.SqlClient Örnek VeriTabanı

using System; using System.Data; using Microsoft.Data.SqlClient;//Install-Package Microsoft.Data.SqlClient using System.Windows.Forms; namespace Sql_Try {     public partial class Form1 : Form     {         SqlConnection sqlVT = new SqlConnection(@"Data Source=DESKTOP-0B1GIRL;Initial Catalog=My_Form;Integrated Security=True;Encrypt=True;Trust Server Certificate=True");         public Form1()         {             InitializeComponent();         }         private void button2_Click(object sender, EventArgs e)         {             Application.Exit();         }         private void button1_Click(object sender, EventArgs e)         {             string Name = textBox_isim.Text;         ...