Quantcast
Channel: C#nedir?com Forum
Viewing all articles
Browse latest Browse all 613

Visual C# : C# Görsel Programlama Ödev Yardım [C# OleDb Excel]

$
0
0
Author: muratercan07
Subject: C# Görsel Programlama Ödev Yardım [C# OleDb Excel]
Posted: 04 Aralık 2017 at 01:04

Arkadaşlar merhaba. Görsel Programlama'da bir ödevimiz var. Yaparsak Final puanına 5 puan katkısı olacak. Ödevimiz ise şöyle.

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 WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button3_Click(object sender, EventArgs e)
        {
            OleDbConnection baglanti = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
            Data Source=daf.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 button1_Click(object sender, EventArgs e)
        {
            OleDbCommand komut = new OleDbCommand();
            OleDbConnection baglanti = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=daf.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 button2_Click(object sender, EventArgs e)
        {
            OleDbCommand komut = new OleDbCommand();
            OleDbConnection baglanti = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=daf.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();
        }
    }
}

Kodları kullanarak Excel ile bağlantı oluşturduk. Verileri getirebiliyoruz, yeni veri ekleyebiliyoruz ve verileri güncelleyebiliyoruz.

Şimdi ödevimiz ise Excel'e Veri ekler iken aynı T.C. Kimlik Numarası ile kayıt yapıldığında programın hata verip böyle bir kayıt zaten var mesajı vermesi gerekiyor. Fakat bir türlü bunu yapamadık. Yardımlarınızı bekliyorum. Şimdiden Teşekkürler. 

Viewing all articles
Browse latest Browse all 613

Trending Articles