NYP-2 2016 5. Hafta Uygulamaları

nyp2_2016_hafta5_ornek1

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 MySql.Data.MySqlClient;

namespace depo
{
    public partial class Form1 : Form
    {
        MySqlDataAdapter da;
        DataTable dt;

        public Form1()
        {
            InitializeComponent();

            MySqlConnectionStringBuilder bag = new MySqlConnectionStringBuilder();
            bag.Server = "localhost";
            bag.UserID = "root";
            bag.Password = "123456";
            bag.Database = "hafta5_16_1";

            MySqlConnection conn = new MySqlConnection(bag.ToString());
            MySqlCommand cmd_sel = new MySqlCommand("select id,isim,miktar from depo;", conn);
            MySqlCommand cmd_ins = new MySqlCommand("insert into depo(isim,miktar) values (@isim,@miktar);",conn);
            cmd_ins.Parameters.Add("@isim", MySqlDbType.VarChar, 45, "isim");
            cmd_ins.Parameters.Add("@miktar", MySqlDbType.Int32, 11, "miktar");

            MySqlCommand cmd_del = new MySqlCommand("delete from depo where id=@id", conn);
            cmd_del.Parameters.Add("@id", MySqlDbType.Int32, 11, "id");

            MySqlCommand cmd_upd = new MySqlCommand("update depo set isim=@isim, miktar=@miktar where id=@id",conn);
            cmd_upd.Parameters.Add("@id", MySqlDbType.Int32, 11, "id");
            cmd_upd.Parameters.Add("@isim", MySqlDbType.VarChar, 45, "isim");
            cmd_upd.Parameters.Add("@miktar", MySqlDbType.Int32, 11, "miktar");

            da = new MySqlDataAdapter();
            da.SelectCommand = cmd_sel;
            da.InsertCommand = cmd_ins;
            da.DeleteCommand = cmd_del;
            da.UpdateCommand = cmd_upd;

            verigetir();
        }

        private void bekle_Click(object sender, EventArgs e)
        {
            int miktar;
            bool kontrol = Int32.TryParse(tmiktar.Text, out miktar);

            if (kontrol && turun.Text.Length > 0)
            {

                try
                {
                    DataRow dr = dt.NewRow(); // mevcut tablodan yeni bir satir turettik
                    dr["isim"] = turun.Text;
                    dr["miktar"] = miktar;
                    dt.Rows.Add(dr); // olusturulan yeni satir tabloya eklendi
                    da.Update(dt);
                    turun.Clear();
                    tmiktar.Clear();

                }
                catch
                { 
                }

                verigetir();
            }



        }


        private void verigetir()
        {
            try
            {
                dt = new DataTable();
                da.Fill(dt);
                lurun.Items.Clear();
                lmiktar.Items.Clear();

                int satirsay = dt.Rows.Count;
                for (int i = 0; i < satirsay; i++)
                {
                    lurun.Items.Add(dt.Rows[i]["isim"]);
                    lmiktar.Items.Add(dt.Rows[i]["miktar"]);
                }
            }
            catch { 
            
            }
        
        
        }

        private void bsil_Click(object sender, EventArgs e)
        {

            int index = lurun.SelectedIndex;
            if (index > -1)
            {
                try
                {
                    dt.Rows[index].Delete();
                    da.Update(dt);
                }
                catch
                { 
                 
                }
                verigetir();
            }



        }

        private void bguncelleme_Click(object sender, EventArgs e)
        {
            int index = lurun.SelectedIndex;
             int miktar;
            bool kontrol = Int32.TryParse(tmiktar.Text, out miktar);

            if (kontrol && index>-1 && turun.Text.Length > 0)
            {

                try
                {
                    DataRow dr = dt.Rows[index]; // mevcut satiri getirdik
                    dr["isim"] = turun.Text;
                    dr["miktar"] = miktar;
                    da.Update(dt);
                    turun.Clear();
                    tmiktar.Clear();
                    verigetir();
                }
                catch
                { 
                    //
                }
            }


        }

    }
}
CREATE SCHEMA `hafta5_16_1` DEFAULT CHARACTER SET utf8 COLLATE utf8_turkish_ci ;
CREATE TABLE `hafta5_16_1`.`depo` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '',
  `isim` VARCHAR(45) NULL COMMENT '',
  `miktar` INT NULL COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '');

 

NYP-2 2016 5. Hafta Uygulamaları

Share