Reader Level:
ARTICLE

SQL Server Database Explorer

Posted by Ajit Kanada Articles | ADO.NET March 20, 2002
This application connects to a running SQL Server 2000 and displays all the available databases in it.
  • 0
  • 0
  • 21547
Download Files:
 

Using DataSets

This application connects to a running SQL Server 2000 and displays all the available databases in it.

On selecting the database it shows all the User Tables in that database in a ComboBox. After selecting the table it shows the Table in DataGrid.If there are any relations present between tables it shows them in the DataGrid. Here I have shown relations for only Order,OrderDetails and Customers Table in Northwind Database.

By modifying the code it can be used to show relation in all the tables.

Application requires

.NET Framework version Beta2 or final
SQLServer2000

How to run

In SQLServer2000 select the NorthwindDatabase choose Order Details table and rename it to OrderDetails(Remove the space between Order and Details ).

Copy this file and save as DataBaseExplorer.cs

Compile it on Command prompt as
csc /r:System.dll;System.data.dll;System.drawing.dll DataBaseExplorer.cs

Then run as DataBaseExplorer

To see the relation between the tables you have to select the tables Orders,OrederDetails and Customers. Only after selecting these tables u can see Customers table showing the relationship as shown in the following figure.

You can explore and view any table in the connected SQLServer. What you learn

  • Using DataSets
  • Event Handling
  • Building WinForms Application

How it works

variables used for explanation

  • lblDatabases- Label
  • cmbDatabases-ComboBox to display database names
  • lblTables-Label
  • myDataGrid -DataGrid to display selected Table

The WinForm contains one label,two CombBoxes and a DataGrid to show the tables.In the constructor add all these components to Form using

this.Controls.AddRange( new Control [] {lblDatabases,cmbDatabases,lblTables,cmbTables,myDataGrid });

When application runs we bind the DataGrid to a default table in default database using

cmbDatabases.DataSource = ajitDB.DBWin.getDataSet(null,null,null);
cmbDatabases.DisplayMember = "sysdatabases.name";

It shows master table as selected table.We write a gereric getDataSet method to return the appropriate DataSet based on SQL query, connection string and table name.We have some utility methods to carryout validations etc..This application uses a single DataSet object.To avoid adding duplicate tables to a Dataset we have a method isDuplicate to check whether table already exists in a DataSet.

Based on the selection of either a database or a table the SQL query is generated dynamically.Based on this query DataGrid is populated.

Enhancements

This application can be modified to update the Tables. By making it more generic we can use this application to connect to any SQLServer. Here name of the SQLServer is hard coded.

COMMENT USING

Trending up