C# Access Ekle Görüntüle Güncelle ve Sil Komutları
Visual Studio 2017 ile C Sharp ta Kullanıcı Formu ile Access te veri ekleme, görüntüleme, güncelleme ve silme komutlarını anlatacağız. Visual Studio da bir C# formu oluşturup içene Veriler ile işlem yapacağımız sırayı belirleyen bir Combobox ekleyelim. Combobox seçimine göre Access tablosundaki yapacağımız işleme göre veriler Textbox lara aktarılacak.Daha sonra Access Tablo Sütun sayısı kadar Textbox ekleyelim. Biz Ad, Soyad ve Parola şeklinde üç sütunu da işlem yapacağımız için 3 tane Textbox ekleyeceğiz ve tüm bu işlemleri gerçekleştirecek olan bir butonları ekleyelim. Bunları yaptıktan sonra Access te bir tablo oluşturup Sıra_No adındaki sütunu Primary Key olarak belirleyelim. Form alanında iken komut butonuna çift tıklayarak açılan ekran da aşağıdaki kodları form kod alanına yazalım.
C# Access Ekle Görüntüle Güncelle ve Sil Tablo Tasarımı
C# Access Ekle Görüntüle Güncelle ve Sil Tablo
C# Access Ekle Görüntüle Güncelle ve Sil Form
C# Uygulama Kodu
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.Runtime.InteropServices;
using System.Data.OleDb;
namespace Acsess_Uygulamalar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
private extern static void ReleaseCapture();
[DllImport("user32.DLL", EntryPoint = "SendMessage")]
private extern static void SendMessage(System.IntPtr hwnd, int wmsg, int wparam, int lparam);
OleDbConnection mdbBaglantisi = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\.....\Documents\Access_uygulamalar.mdb");
private void btnKapat_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void pnlKontrol_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, 0x112, 0xf012, 0);
}
private void cmboxSıraNo_SelectedIndexChanged(object sender, EventArgs e)
{
mdbBaglantisi.Open();
OleDbCommand verioku = new OleDbCommand("select * from Access_uygulamalar where Sıra_No = " + cmboxSıraNo.Text.ToString() + "", mdbBaglantisi);
verioku.ExecuteNonQuery();
OleDbDataReader oku;
oku = verioku.ExecuteReader();
while (oku.Read())
{
txtAd.Text = oku["Ad"].ToString();
txtSoyad.Text = oku["Soyad"].ToString();
txtParola.Text = oku["Parola"].ToString();
}
oku.Close();
mdbBaglantisi.Close();
}
private void cmboxSıraNo_Click(object sender, EventArgs e)
{
cmboxSıraNo.Items.Clear();
mdbBaglantisi.Open();
OleDbCommand verioku = new OleDbCommand("select * from Access_uygulamalar ", mdbBaglantisi);
OleDbDataReader getir;
getir = verioku.ExecuteReader();
while (getir.Read())
{
cmboxSıraNo.Items.Add(getir["Sıra_No"].ToString());
}
getir.Close();
mdbBaglantisi.Close();
}
private void btnEkle_Click(object sender, EventArgs e)
{
if (mdbBaglantisi.State == ConnectionState.Closed)
{
mdbBaglantisi.Open();
OleDbCommand veriekle = new OleDbCommand("insert into Access_uygulamalar (Sıra_No, Ad, Soyad, Parola) values (@Sıra_No,@Ad,@Soyad,@Parola)", mdbBaglantisi);
veriekle.Parameters.AddWithValue("@Sıra_No", cmboxSıraNo.Text);
veriekle.Parameters.AddWithValue("@Ad", txtAd.Text);
veriekle.Parameters.AddWithValue("@Soyad", txtSoyad.Text);
veriekle.Parameters.AddWithValue("@Parola", txtParola.Text);
veriekle.ExecuteNonQuery();
mdbBaglantisi.Close();
}
else
{
MessageBox.Show("Veri Ekleme Başarısız !");
}
}
private void btnGörüntüle_Click(object sender, EventArgs e)
{
mdbBaglantisi.Open();
OleDbCommand verioku = new OleDbCommand("select * from Access_uygulamalar where Sıra_No = " + cmboxSıraNo.Text.ToString() + "", mdbBaglantisi);
verioku.ExecuteNonQuery();
OleDbDataReader oku;
oku = verioku.ExecuteReader();
while (oku.Read())
{
txtAd.Text = oku["Ad"].ToString();
txtSoyad.Text = oku["Soyad"].ToString();
txtParola.Text = oku["Parola"].ToString();
}
oku.Close();
mdbBaglantisi.Close();
}
private void btnGüncelle_Click(object sender, EventArgs e)
{
mdbBaglantisi.Open();
OleDbDataAdapter mdbadaptör = new OleDbDataAdapter("select count(*) from Access_uygulamalar where Sıra_No = " + cmboxSıraNo.Text + "", mdbBaglantisi);
DataTable dtablo = new DataTable();
mdbadaptör.Fill(dtablo);
OleDbCommand mdbadaptörG = new OleDbCommand("update Access_uygulamalar set Ad = '" + txtAd.Text.ToString() + "' ,Soyad ='" + txtSoyad.Text.ToString() + "',Parola ='" + txtParola.Text.ToString() + "' where Sıra_No =" + cmboxSıraNo.Text + "", mdbBaglantisi);
mdbadaptörG.ExecuteNonQuery();
mdbBaglantisi.Close();
}
private void btnSil_Click(object sender, EventArgs e)
{
mdbBaglantisi.Open();
OleDbCommand verisil = new OleDbCommand("delete from Access_uygulamalar where Sıra_No = " + cmboxSıraNo.Text + "", mdbBaglantisi);
verisil.ExecuteNonQuery();
mdbBaglantisi.Close();
}
}
}
Yorum Gönder
Yazı Hakkındaki Düşünceleriniz...