NYP-2 2016 10. Hafta Uygulamaları-2

nyp2_2016_hafta10_2

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 datagridw
{
    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 = "hafta8_16_1_1";

            MySqlConnection conn = new MySqlConnection(bag.ToString());

            MySqlCommand cmd_sel = new MySqlCommand("select id,isimler,telefon from liste;", conn);
            MySqlCommand cmd_ins = 
                new MySqlCommand("insert into liste(isimler,telefon) values(@isimler,@telefon);", conn);
            cmd_ins.Parameters.Add("@isimler", MySqlDbType.VarChar, 45, "isimler");
            cmd_ins.Parameters.Add("@telefon", MySqlDbType.VarChar, 45, "telefon");
            
            MySqlCommand cmd_del = 
                new MySqlCommand("delete from liste where id=@id;", conn);
            cmd_del.Parameters.Add("@id", MySqlDbType.Int32, 11, "id");
           
            MySqlCommand cmd_upd = 
                new MySqlCommand("update liste set isimler=@isimler, telefon=@telefon where id=@id", conn);
            cmd_upd.Parameters.Add("@id", MySqlDbType.Int32, 11, "id");
            cmd_upd.Parameters.Add("@isimler", MySqlDbType.VarChar, 45, "isimler");
            cmd_upd.Parameters.Add("@telefon", MySqlDbType.VarChar, 45, "telefon");

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

            verigetir();
            

        }

        private void verigetir()
        {
            try
            {
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                // dataGridView1.Columns[0].ReadOnly = true; // id kolonu sadece okunabilir olsun
                dataGridView1.Columns[0].Visible = false; // id kolonu gizle/goster
                dataGridView1.Columns[1].HeaderText = "Ad Soyad";
                dataGridView1.Columns[2].HeaderText = "Telefon No";

                //dataGridView1.AllowUserToAddRows = false; // yeni satir eklemeyi iptal et
                dataGridView1.AllowUserToDeleteRows = false; // satir silme aktif/pasif
            }
            catch { 
            }

        
        }

        private void byenile_Click(object sender, EventArgs e)
        {
            verigetir();
        }

        private void bkaydet_Click(object sender, EventArgs e)
        {
            try
            {
                da.Update(dt);
                verigetir();
            }
            catch { }


        }
    }
}

NYP-2 2016 Hafta-8 – 2

Share