C#'tan Veri Tabanına Bağlantı

C#'ta veri tabanına bağlanıp c# kodlarıyla insert, delete, update işlemlerini göstereceğim. Verileri textboxdan girdireceğiz.

C#'ta veri tabanına bağlanıp c# kodlarıyla insert, delete, update işlemlerini göstereceğim. Verileri textboxdan girdireceğiz.


using System;

using System.Collections.Generic;

using System.Drawing;

using System.Windows.Forms;

using System.Data;

using MySql.Data.MySqlClient;



namespace veriyikay

{

    

    public partial class MainForm : Form

    {

    

        private TextBox txt1;

        private TextBox txt2;

        private TextBox txt3;

        private Button btn;

        private Button btn1;

        private Button btn2;

        

        public MainForm()

        {

            txt1=new TextBox();

            txt1.Location=new Point(16,16);

            txt2=new TextBox();

            txt2.Location=new Point(16,46);

            txt3=new TextBox();

            txt3.Location=new Point(16,76);

            btn=new Button();

            btn.Location=new Point(16,116);

            btn.Text="Kaydet";

            btn.Click+=new EventHandler(erg);

            btn1=new Button();

            btn1.Location=new Point(116,116);

            btn1.Text="Sil";

            btn1.Click+=new EventHandler(ergun);

            btn2=new Button();

            btn2.Location=new Point(216,116);

            btn2.Text="Güncelle";

            btn2.Click+=new EventHandler(ergunceliker);

            Controls.AddRange(new Control[]{txt1,txt2,txt3,btn,btn1,btn2});

            

        }

        private void erg(object sender, EventArgs e)

        {

            

            string myConnString="Server=localhost;"+"Database=ergün;"+"User ID=root;"+"Password=******;";

            MySqlConnection myConnection=new MySqlConnection(myConnString);

            string myInsertQuery="insert into ogrenci (no,ad,soyad) values ('" + txt1.Text + "','" + txt2.Text + "','" + txt3.Text + "')";

            MySqlCommand myCommand=new MySqlCommand(myInsertQuery,myConnection);

            myConnection.Open();

            myCommand.ExecuteNonQuery();

            myConnection.Close();

            

        }

        private void ergun(object sender, EventArgs e)

        {

            string myConnString="Server=localhost;"+"Database=ergün;"+"User ID=root;"+"Password=******;";

            MySqlConnection myConnection=new MySqlConnection(myConnString);

            string myDeleteQuery="delete from ogrenci where no='" +txt1.Text + "'";

            MySqlCommand myCommand=new MySqlCommand(myDeleteQuery,myConnection);

            myConnection.Open();

            myCommand.ExecuteNonQuery();

            myConnection.Close();

        }

        private void ergunceliker(object sender, EventArgs e)

        {

            string myConnString="Server=localhost;"+"Database=ergün;"+"User ID=root;"+"Password=******;";

            MySqlConnection myConnection=new MySqlConnection(myConnString);

            string myUpdateQuery=" update ogrenci set ad='" + txt2.Text + "',soyad='"+ txt3.Text +"' where no='"+ txt1.Text +"'";

            MySqlCommand myCommand=new MySqlCommand(myUpdateQuery,myConnection);

            myConnection.Open();

            myCommand.ExecuteNonQuery();

            myConnection.Close();

        }

    }

        class ergün {

        public    static void Main() {

       Application.Run(new MainForm());

        }

    }

}

  • Etiketler;
Yorum Yaz

Yorum yazabilmek için üye girişi yapmanız gerekiyor!

Yukarı Git