Retrieve database schema (meta data information) using ADO.NET

by Jagadish Pulakhandam on 9/6/2011 1:10:04 PM
Rated 0 from 0 votes
Brief: Demonstrates on retrieving database object schema information using ADO.NET
Posted to: Retrieving Database Schema Information in ADO.NET
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Demonstrates on retrieving database object schema information.  The objects include Databases, Tables, Columns, Procedures etc.

Screen shot:



Source Code:

01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Linq;
07.using System.Text;
08.using System.Windows.Forms;
09.using System.Data.SqlClient;
10. 
11.namespace CSRetrieveSchemaInfo
12.{
13.    public partial class Form1 : Form
14.    {
15.        string CONNSTR = "Data Source=.\\sql2k8; Initial Catalog=Northwind; User Id=sa; Password=eXpress2008";
16. 
17.        public Form1()
18.        {
19.            InitializeComponent();
20.        }
21. 
22.        private void btnMetaData_Click(object sender, EventArgs e)
23.        {
24.            using (SqlConnection cn = new SqlConnection(CONNSTR))
25.            {
26.                cn.Open();
27.                DataTable dt = cn.GetSchema();
28.                this.DataGridView1.DataSource = dt;
29.            }
30.        }
31. 
32.        private void btnDatabases_Click(object sender, EventArgs e)
33.        {
34.            using (SqlConnection cn = new SqlConnection(CONNSTR))
35.            {
36.                cn.Open();
37.                DataTable dt = cn.GetSchema("Databases");
38.                this.DataGridView1.DataSource = dt;
39.            }
40.        }
41. 
42.        private void btnTables_Click(object sender, EventArgs e)
43.        {
44.            using (SqlConnection cn = new SqlConnection(CONNSTR))
45.            {
46.                cn.Open();
47.                DataTable dt = cn.GetSchema("Tables");
48.                this.DataGridView1.DataSource = dt;
49.            }
50.        }
51. 
52.        private void btnColumns_Click(object sender, EventArgs e)
53.        {
54.            using (SqlConnection cn = new SqlConnection(CONNSTR))
55.            {
56.                cn.Open();
57.                DataTable dt = cn.GetSchema("Columns");
58.                this.DataGridView1.DataSource = dt;
59.            }
60.        }
61. 
62.        private void btnProcs_Click(object sender, EventArgs e)
63.        {
64.            using (SqlConnection cn = new SqlConnection(CONNSTR))
65.            {
66.                cn.Open();
67.                DataTable dt = cn.GetSchema("Procedures");
68.                this.DataGridView1.DataSource = dt;
69.            }
70.        }
71. 
72.        private void btnViews_Click(object sender, EventArgs e)
73.        {
74.            using (SqlConnection cn = new SqlConnection(CONNSTR))
75.            {
76.                cn.Open();
77.                DataTable dt = cn.GetSchema("Views");
78.                this.DataGridView1.DataSource = dt;
79.            }
80.        }
81.    }
82.}

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following