14 Aralık 2014 Pazar

C# Örnek Veri Tabanı(DateBase Connect) Bağlantısı ve İşlemleri

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=C:/Users/la/Desktop/rehber.accdb");

//Rehber adlı database ye ulaşıyor.
        DataSet se = new DataSet();
        CurrencyManager cm;
        private void Form1_Load(object sender, EventArgs e)
        {
            OleDbDataAdapter adp = new OleDbDataAdapter("select*from tel", con);
            adp.Fill(se, "tel");
            cm = (CurrencyManager)BindingContext[se.Tables[0]];
            //kontrollerin databaseye bağlanması
            dataGridView1.DataSource = se.Tables[0];
         
            textBox1.DataBindings.Add("text", se.Tables[0], "ad");
            textBox2.DataBindings.Add("text", se.Tables[0], "soyad");
            textBox3.DataBindings.Add("text", se.Tables[0], "tel");
            textBox4.DataBindings.Add("text", se.Tables[0], "adres");
//Burada ise databasedeki bilgileri dataGridView e ve textboxlara aktarıyor.

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
//Eğer yol kapalı ise açıyor zaten açık ise burayı atlıyor.
            }
            OleDbCommand com=new OleDbCommand("insert into tel(ad,soyad,tel,adres)values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"')",con);
            com.ExecuteNonQuery();
            MessageBox.Show("Kayıt Eklendi");
            con.Close();
            //datagrid güncelliyor ve güncel bilgileri gösteriyor.
            se.Clear();
            OleDbDataAdapter adp = new OleDbDataAdapter("select*from tel", con);
            adp.Fill(se, "tel");
            dataGridView1.DataSource = se.Tables[0];

        }

        private void ilkkyt_Click(object sender, EventArgs e)
        {
            cm.Position = 0; //dataGridView de ilk kayıta gider.
        }

        private void button2_Click(object sender, EventArgs e)
        {
            cm.Position = cm.Position - 1;  //dataGridView de geri gider.
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cm.Position = cm.Position + 1;  //dataGridView de ileri gider.
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int a = se.Tables["tel"].Rows.Count; //dataGridView de son kayıta gider.
            cm.Position = a - 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear(); // yeni kayıt yapmak için textboxları temizler.
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
         
        }

        private void button6_Click(object sender, EventArgs e)
        {
            // sil butonu
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            OleDbCommand com = new OleDbCommand("delete * from tel where ad='" + textBox1.Text  + "'", con);
            com.ExecuteNonQuery();
            MessageBox.Show("Kayıt Silindi");
            con.Close();
            se.Clear();
            OleDbDataAdapter adp = new OleDbDataAdapter("select * from tel", con);
            adp.Fill(se, "tel");
            dataGridView1.DataSource = se.Tables[0];
        }

        private void button7_Click(object sender, EventArgs e)
        {
//Güncelleme Butonu
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            OleDbCommand com = new OleDbCommand("update tel set ad='" + textBox1.Text + "',soyad='" + textBox2.Text + "',tel='" + textBox3.Text + "',adres='" + textBox4.Text + "'where ad='" + textBox1.Text + "'", con);
            com.ExecuteNonQuery();
            MessageBox.Show("Kayıt Güncelendi");
            con.Close();
                //datagrid view
            se.Clear();
            OleDbDataAdapter adp = new OleDbDataAdapter("select * from tel", con);
            adp.Fill(se, "tel");
            dataGridView1.DataSource = se.Tables[0];

        }
    }
}

Paylaş:

0 yorum:

Yorum Gönder

Popüler

Son

Kategoriler

Arşiv