Merhaba arkadaşlar bu
makalemizde veritabanında bulunan 
tabloların  isimlerini Combobox ta
 göstereceğiz. Combobox ta seçmiş
olduğumuz tabloya ait  verileri dataGridView
nesnesinde göstereceğiz.
Screenshot
 
 
Şekil
1
Form1.cs
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection baglan;
        SqlDataAdapter da;
        DataSet ds;
        private void Form1_Load(object sender, EventArgs e)
        {
            baglan = new SqlConnection("Data
Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|kategori.mdf;Integrated
Security=True;Connect Timeout=30");
            baglan.Open();
            DataTable dt = baglan.GetSchema("Tables");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                comboBox1.Items.Add(dt.Rows[i]["table_name"]);
            }
            baglan.Close();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string table = comboBox1.Text;
            string sql = "Select *
From " + "[" + table +"]";
            da = new SqlDataAdapter(sql, baglan);
            ds = new DataSet();
            baglan.Open();
            da.Fill(ds, table);
            dataGridView1.DataSource =
ds.Tables[table];
            baglan.Close();
        }
    }
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede
görüşmek üzere. Bahadır ŞAHİN 

0 comments:
Yorum Gönder