Windows application for database information

This example deals with displaying databases installed on the current computer, the tables of the selected database, and the data of the selected table.
 
For these 2 comboboxes and one datagridview has been taken in a windows application.
 
The queries that have been used are as follows.
  1. select name from sysdatabases - It displays databases installed on the current server.
  2. select table_name from information_schema.tables where table_type='base table' and table_catalog=@a"
    It displays tables of the selected database.
    The tables displayed are user-defined, but for the system-defined databases, some system-defined tables are also displayed.
  3. To display the data of the selected tables, the appropriate query has been written.
Code of Form1.cs file
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10. namespace windowsdata {  
  11.     public partial class Form1: Form {  
  12.         SqlConnection cn;  
  13.         SqlCommand cmd;  
  14.         public Form1() {  
  15.             InitializeComponent();  
  16.         }  
  17.         private void Form1_Load(object sender, EventArgs e) {  
  18.             //all the databases on the server  
  19.             cn = new SqlConnection("server=.;uid=sa;pwd=1234;database=master");  
  20.             cmd = new SqlCommand("select name from sysdatabases", cn);  
  21.             cn.Open();  
  22.             SqlDataReader dr = cmd.ExecuteReader();  
  23.             while (dr.Read() == true) {  
  24.                 comboBox1.Items.Add(dr[0].ToString());  
  25.             }  
  26.             dr.Close();  
  27.             cn.Close();  
  28.         }  
  29.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {  
  30.             //tables of the selected database  
  31.             comboBox2.Text = "";  
  32.             //clear the combobox of earlier data  
  33.             if (comboBox2.Items.Count != 0) {  
  34.                 comboBox2.Items.Clear();  
  35.             }  
  36.             //clear the datagridview of earlier data  
  37.             dataGridView1.DataSource = null;  
  38.             string s = comboBox1.SelectedItem.ToString();  
  39.             cn = new SqlConnection("server=.;uid=sa;pwd=1234;database=" + s);  
  40.             cmd = new SqlCommand("select table_name from information_schema.tables where table_type='base table' and table_catalog=@a", cn);  
  41.             cmd.Parameters.AddWithValue("@a", s);  
  42.             cn.Open();  
  43.             SqlDataReader dr = cmd.ExecuteReader();  
  44.             while (dr.Read() == true) {  
  45.                 comboBox2.Items.Add(dr[0].ToString());  
  46.             }  
  47.             dr.Close();  
  48.             cn.Close();  
  49.         }  
  50.         private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) {  
  51.             //data of the selected table  
  52.             string s = comboBox2.SelectedItem.ToString();  
  53.             SqlDataAdapter da = new SqlDataAdapter("select * from" + " " + s, cn);  
  54.             DataTable dt = new DataTable();  
  55.             da.Fill(dt);  
  56.             dataGridView1.DataSource = dt;  
  57.         }  
  58.     }  

The snapshot of the application is as follows.
 
1.gif
 
All the best.


Similar Articles