Bu örnekte C# Class (sınıf) yapısını kullanarak SQL veritabanı bağlantısı oluşturmayı, tablo verilerini List içine almayı, List içindeki verilerle işlem yapmayı öğreneceksiniz.
Örneğimizi kısaca açıklayalım.
Örneğimizde SQL Northwind veritabanına bağlanarak Customer tablosundaki verileri Class-List kullanarak DatagridView içinde listeleyeceğiz.
2. adımda List içine çektiğimiz bu verileri kullanarak Country alanındaki ülkeleri benzersiz bir şekilde Combobox içine çekeceğiz.
Son olarak ComboBox seçimine göre verileri filtreleyerek DataGridView içinde görüntüleyeceğiz.
Örneğimizde iki adet sınıf oluşturacağız. Class oluşturmak için Solution Explorer penceresinde projenizin (ClassListCustomer) üstünde sağ tıklayarak Add-Class adımlarını takip edebilirsiniz.
Kodlarımız aşağıdaki gibi olacaktır.
Customer.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClassListCustomer { internal class Customer { string customerId; string companyName; string contactName; string contactTitle; string address; string city; string region; string postalCode; string country; string phone; string fax; public string CustomerId { get => customerId; set => customerId = value; } public string CompanyName { get => companyName; set => companyName = value; } public string ContactName { get => contactName; set => contactName = value; } public string ContactTitle { get => contactTitle; set => contactTitle = value; } public string Address { get => address; set => address = value; } public string City { get => city; set => city = value; } public string Region { get => region; set => region = value; } public string PostalCode { get => postalCode; set => postalCode = value; } public string Country { get => country; set => country = value; } public string Phone { get => phone; set => phone = value; } public string Fax { get => fax; set => fax = value; } } } |
CustomerProvider.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace ClassListCustomer { internal class CustomerProvider { SqlConnection con=new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"); public List<Customer> customers { get; set; } public CustomerProvider() { customers=new List<Customer>(); GetCustomers(); } private void GetCustomers() { con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Customer customer = new Customer(); customer.CustomerId=dr[0].ToString(); customer.CompanyName=dr[1].ToString(); customer.ContactName=dr[2].ToString(); customer.ContactTitle=dr[3].ToString(); customer.Address=dr[4].ToString(); customer.City=dr[5].ToString(); customer.Region=dr[6].ToString(); customer.PostalCode=dr[7].ToString(); customer.Country=dr[8].ToString(); customer.Phone=dr[9].ToString(); customer.Fax=dr[10].ToString(); customers.Add(customer); } dr.Close(); con.Close(); } public List<string> FillCombobox() { return customers.Select(x=>x.Country).Distinct().ToList(); } public List<Customer> FilterCustomer(string country) { return customers.Where(x => x.Country == country).ToList(); } } } |
Form1.cs için kodlarımız:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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; namespace ClassListCustomer { public partial class Form1 : Form { public Form1() { InitializeComponent(); } CustomerProvider provider = new CustomerProvider(); private void Form1_Load(object sender, EventArgs e) { dgvCustomers.DataSource = provider.customers; cmbCountry.DataSource=provider.FillCombobox(); } private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e) { dgvCustomers.DataSource = provider.FilterCustomer(cmbCountry.Text); } private void btnListele_Click(object sender, EventArgs e) { cmbCountry.SelectedIndex = -1; dgvCustomers.DataSource = provider.customers; } } } |
Örneğe ait videoyu kanalımızdan izleyebilirsiniz.
Merhaba hocam acaba combobox dan seçilen isim ve soyisim sql column e göre datagridviewe sql veri listeleme olayını yapabilir misiniz acaba ?