rq

rqrq
mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.2 kB
1
Indexable
Never
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 System.Data.OleDb;

namespace _6_fatih_bellek
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        OleDbConnection baglanti = new OleDbConnection();
        OleDbCommand komut = new OleDbCommand();
        OleDbDataAdapter veri_düzenleyici = new OleDbDataAdapter();

        private void Form1_Load(object sender, EventArgs e)
        {
            veri_yukleyici();
            this.dgv_database.AllowUserToAddRows = false;
            this.dgv_database.AllowUserToDeleteRows = false;
            this.dgv_database.MultiSelect = false;
            this.dgv_database.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dgv_database.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically;
            dgv_database.Columns["Kimlik"].Visible = false;


        }
        private void veri_yukleyici()
        {
            string sorgu = "SELECT * FROM Ogrenci_tablo";
            baglanti.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.mdb");
            try
            {
                baglanti.Open();
                komut.Connection = baglanti;
                komut.CommandText = sorgu;
                veri_düzenleyici.SelectCommand = komut;
                DataTable veri_tablosu = new DataTable();
                veri_düzenleyici.Fill(veri_tablosu);
                dgv_database.DataSource = veri_tablosu;
            }
            catch (Exception error)
            {

                MessageBox.Show("Bağlantı hatası");
            }
            finally { baglanti.Close(); }
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            string sorgu = "INSERT INTO Ogrenci_tablo ([User_name],[User_pass]) VALUES (@User_name,@User_pass)";
            baglanti.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.mdb");
            try
            {
                baglanti.Open();

                komut.Connection = baglanti;
                komut.CommandText = sorgu;

                komut.Parameters.Clear();
                komut.Parameters.Add("@User_name", txt_username.Text);
                komut.Parameters.Add("@User_pass", txt_password.Text);

                komut.ExecuteNonQuery();


            }
            catch (Exception error)
            {

                MessageBox.Show("Bağlantı hatası");
            }
            finally { baglanti.Close(); }

            veri_yukleyici();
            if (dgv_database.Rows.Count>0)
            {
                dgv_database.Rows[dgv_database.Rows.Count - 1].Selected = true;
            }
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            string sorgu = "DELETE from Ogrenci_tablo WHERE  ID= @ID";
            baglanti.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.mdb");
            try
            {
                baglanti.Open();

                komut.Connection = baglanti;
                komut.CommandText = sorgu;

                komut.Parameters.Clear();
                komut.Parameters.Add("ID", dgv_database.SelectedRows[0].Cells[0].Value);

                komut.ExecuteNonQuery();


            }
            catch (Exception error)
            {

                MessageBox.Show("Bağlantı hatası");
            }
            finally { baglanti.Close(); }

            int silinen_index = dgv_database.SelectedRows[0].Index;

            veri_yukleyici();

            if (silinen_index > 0)
            {
                dgv_database.Rows[silinen_index - 1].Selected = true;
            }
        }
    }
}