SIGN UP MEMBER LOGIN:    
ARTICLE

Windows application for database information

Posted by Dinesh Dugg Articles | ADO.NET in C# January 14, 2011
This example deals with displaying databases installed on the current computer, the tables of the selected database and the data of the selected table.
Reader Level:
Download Files:
 

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 this 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

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

namespace windowsdata
{
    public partial class Form1 : Form
    {
        SqlConnection cn;
        SqlCommand cmd;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //all the databases on the server
            cn = new SqlConnection("server=.;uid=sa;pwd=1234;database=master");
            cmd = new SqlCommand("select name from sysdatabases", cn);
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
                comboBox1.Items.Add(dr[0].ToString());
            }
            dr.Close();
            cn.Close();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //tables of the selected database
            comboBox2.Text = "";
            //clear the combobox of earlier data
            if (comboBox2.Items.Count != 0)
            {
                comboBox2.Items.Clear();
            }
            //clear the datagridview of earlier data
            dataGridView1.DataSource = null;
            string s = comboBox1.SelectedItem.ToString();
            cn = new SqlConnection("server=.;uid=sa;pwd=1234;database="+s);
            cmd = new SqlCommand("select table_name from information_schema.tables where table_type='base table' and table_catalog=@a", cn);
            cmd.Parameters.AddWithValue("@a", s);
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
                comboBox2.Items.Add(dr[0].ToString());
            }
            dr.Close();
            cn.Close();
        }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //data of the selected table
            string s = comboBox2.SelectedItem.ToString();
            SqlDataAdapter da = new SqlDataAdapter("select * from" + " " + s, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
    }
}

The snapshot of the application is as follows.

1.gif 

All the best.

Login to add your contents and source code to this article
share this article :
post comment
 

simple and easy to learn...thanks

Posted by Sharath P Jan 25, 2012
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor