C#-Excel Veri Çekme-Ekleme-Güncelleme İşlemleri

C# ile Excel’e veri ekleme-güncelleme ve listeleme işlemleri için tıklayınız.

 

KODLAR:

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 Excel_Ornekleri
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
OleDbConnection baglanti = new OleDbConnection(@”Provider=Microsoft.ACE.OLEDB.12.0;Data Source=veriler.xlsx; Extended Properties=’Excel 12.0 xml;HDR=YES;'”);
baglanti.Open();
OleDbDataAdapter da = new OleDbDataAdapter(“SELECT * FROM [Sayfa1$]”, baglanti);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
baglanti.Close();
}

private void button2_Click(object sender, EventArgs e)
{
OleDbCommand komut = new OleDbCommand();
OleDbConnection baglanti = new OleDbConnection(@”Provider=Microsoft.ACE.OLEDB.12.0;Data Source=veriler.xlsx; Extended Properties=’Excel 12.0 xml;HDR=YES;'”);
baglanti.Open();
komut.Connection = baglanti;
string sql = “Insert into [Sayfa1$] (NUMARA,AD,SOYAD,PROGRAM) values(‘” + maskedTextBox1.Text + “‘,'” + textBox2.Text + “‘,'” + textBox3.Text + “‘,'” + textBox4.Text + “‘)”;
komut.CommandText = sql;
komut.ExecuteNonQuery();
MessageBox.Show(“Veriler Excel Dosyasına Eklenmiştir.”);
baglanti.Close();
}

private void button3_Click(object sender, EventArgs e)
{
OleDbCommand komut = new OleDbCommand();
OleDbConnection baglanti = new OleDbConnection(@”Provider=Microsoft.ACE.OLEDB.12.0;Data Source=veriler.xlsx;Extended Properties=’Excel 12.0 xml;HDR=YES'”);
baglanti.Open();
komut.Connection = baglanti;
string sql = “Update [Sayfa1$] set AD='” + textBox2.Text + “‘,SOYAD='” + textBox3.Text + “‘,PROGRAM='” + textBox4.Text + “‘ WHERE NUMARA=” + maskedTextBox1.Text + “”;
komut.CommandText = sql;
komut.ExecuteNonQuery();
MessageBox.Show(“Güncelleme İşlemi Başarılı…”);
baglanti.Close();
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
maskedTextBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
}
}
}

 

admin