C# Sql Server Combobox’tan Seçilen Tabloya Göre Veri Listeleme

sdkbyrm

webmasterfrm
Üyelik Tarihi
23 Aralık 2020
Mesajlar
813
Beğeniler
1
Ticaret: 0 / 0 / 0
Bu yazımızda C# Windows Form ile Sql Server‘da bulunan veritabanı içinde bulunan Tablo isimlerinin Combobox‘a çekilmesi ve Combobox’tan seçilen Tablo Adı‘ na göre, tablodaki verilerin DataGridView kontrolünde listelenmesini sağlayacağız. Sql Server’da kullanacağımız veritabanının ismi kutuphane olacaktır.

Formumuzun tasarımı aşağıdaki gibi olacaktır.
sql_tablo_secim_2


Kodlarımızı yazmaya başlayalım. Öncelikle;


1
2
3

using System.Data.SqlClient;
ekliyoruz. Daha sonra public olarak gerekli nesnelerimizi oluşturuyoruz.


1
2
3
4
5

SqlConnection baglanti;
SqlDataAdapter da;
DataSet ds;
Form_Load olayında yani Form yüklendiğinde Sql Server‘ da bulunan veritabanının içindeki tablo isimlerinin Combobox kontrolü içine doldurulmasını sağlayalım.


1
2
3
4
5
6
7
8
9
10
11
12
13
14

private void Form1_Load(object sender, EventArgs e)
{
baglanti = new SqlConnection("server=.;Initial Catalog=kutuphane;Integrated Security=SSPI");
baglanti.Open();
DataTable dt = baglanti.GetSchema("Tables");

for (int i = 0; i < dt.Rows.Count; i++)
{
comboBox1.Items.Add(dt.Rows["TABLE_NAME"]);
}
baglanti.Close();
}



Şimdide Combobox‘ta seçilen Tablo ismine göre DataGridView de verilerin listelenmesini sağlayalım. Kodlarımızı comboBox1_SelectedIndexChanged olayına yazıyoruz.


1
2
3
4
5
6
7
8
9
10
11
12
13

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string tablo = comboBox1.Text;
string sorgu="SELECT *FROM "+tablo;
da = new SqlDataAdapter(sorgu, baglanti);
ds = new DataSet();
baglanti.Open();
da.Fill(ds, tablo);
dataGridView1.DataSource = ds.Tables[tablo];
baglanti.Close();
}
sql_tablo_secim_1

Kodlarımızın tamamlanmış hali aşağıdaki gibi olacaktır.


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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sqlTabloListeleme
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection baglanti;
SqlDataAdapter da;
DataSet ds;
private void Form1_Load(object sender, EventArgs e)
{
baglanti = new SqlConnection("server=.;Initial Catalog=kutuphane;Integrated Security=SSPI");
baglanti.Open();
DataTable dt = baglanti.GetSchema("Tables");

for (int i = 0; i < dt.Rows.Count; i++)
{
comboBox1.Items.Add(dt.Rows["TABLE_NAME"]);
}
baglanti.Close();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string tablo = comboBox1.Text;
string sorgu="SELECT *FROM "+tablo;
da = new SqlDataAdapter(sorgu, baglanti);
ds = new DataSet();
baglanti.Open();
da.Fill(ds, tablo);
dataGridView1.DataSource = ds.Tables[tablo];
baglanti.Close();
}
}
}


 
Üst