C# Access Veritabanında Seçilen Alanda Arama Yapma

Bu örneğimizde C# Access veritabanı bağlantısı kurarak radioButton  kontrolleri ile seçili alanda arama işlemini gerçekleşeceğiz. Örneğimizde kutuphane veritabanında bulunan ogrenci tablosunu kullanacağız. Kayıt arama işlemini Öğrencinin adı,soyadı veya sınıfını kullanarak yapacağız. Bu işlemi yaparken radioButton kontrollerinden faydalanacağız. Kullanacağımız ogrenci tablosu ve Form tasarımı aşağıdaki gibi olacaktır. Resimlerin üstüne tıklayarak büyütebilirsiniz.

 

Formumuzda bulunan Tüm Kayıtları Getir butonu ogrenci tablosundaki tüm kayıtların listelenmesini sağlayacak. ARA butonu ise radioButtonlaradan seçili olan alana göre TextBox kontrolüne girilen kaydın filtrelenerek DatagridView kontrolüne aktarımını sağlayacak.

Kodlarımızın tamamı ve ekran görüntüsü aşağıdaki gibi olacaktır.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace alan_radio_arama { public partial class Form1 : Form { public Form1() { InitializeComponent(); } OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=kutuphane.mdb"); OleDbDataAdapter da; DataTable dt; string sql = "SELECT * FROM ogrenci"; void Listele(string aranan) { da = new OleDbDataAdapter(sql, con); dt = new DataTable(); con.Open(); da.Fill(dt); con.Close(); dataGridView1.DataSource = dt; } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if(radioButton1.Checked) { sql = "SELECT *FROM ogrenci WHERE ograd='"+textBox1.Text+"'"; } else if(radioButton2.Checked) { sql = "SELECT *FROM ogrenci WHERE ogrsoyad='" + textBox1.Text + "'"; } else if(radioButton3.Checked) { sql = "SELECT *FROM ogrenci WHERE sinif='" + textBox1.Text + "'"; } else { sql = "SELECT *FROM ogrenci"; } Listele(sql); } private void button2_Click(object sender, EventArgs e) { sql = "SELECT *FROM ogrenci"; Listele(sql); } } }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.OleDb;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms; namespacealan_radio_arama{publicpartial classForm1:Form{publicForm1(){InitializeComponent();} OleDbConnection con=newOleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=kutuphane.mdb");OleDbDataAdapter da;DataTable dt;stringsql="SELECT * FROM ogrenci"; voidListele(stringaranan){da=newOleDbDataAdapter(sql,con);dt=newDataTable();con.Open();da.Fill(dt);con.Close();dataGridView1.DataSource=dt;}privatevoidForm1_Load(objectsender,EventArgse){} privatevoidbutton1_Click(objectsender,EventArgse){if(radioButton1.Checked){sql="SELECT *FROM ogrenci WHERE ograd='"+textBox1.Text+"'";}elseif(radioButton2.Checked){sql="SELECT *FROM ogrenci WHERE ogrsoyad='"+textBox1.Text+"'";}elseif(radioButton3.Checked){sql="SELECT *FROM ogrenci WHERE sinif='"+textBox1.Text+"'";}else{sql="SELECT *FROM ogrenci";} Listele(sql); } privatevoidbutton2_Click(objectsender,EventArgse){sql="SELECT *FROM ogrenci";Listele(sql);}}}

Ekran Çıktısı:

Bunu paylaş:

  • X
  • Facebook

Từ khóa » Visual Basic Arama Butonu Yapma