Introduction

It helps you explore all available databases in a MS SQL Server, tables under them and data contained in those tables. You can select any database (or add existing database) and browse their tables. Select any table and look into data contained in them. We used DataGridView to explore the data.

Why it was required: Well I was exploring a database and I had to move from one table to another, which required issuing commands again and again although I had an option to issue multiple selects, but it was still not soothing.

So here comes DB Invader (DB Invador). We have used Factory pattern and Singleton pattern in the development so that connection pool is effectively utilized. Explore and let us know your findings, suggestion.

It now supports tables in different schemas as well and you can browse for different SQL Servers or instances simultaneously.

Please rate this a 5 star (xxxxx) if you liked it. You can also consider donating us a small amount to help us build more free stuffs.

Building the Sample

Open the solution file. Hit F5 and it will launch itself. You should have a SQL Server so that it works as required.

If you have Database files somewhere then you need to attach them to SQL Server in order to explore them as well. It will hardly take you less than 1 minute to attach DB files.

Description

Selected the servers you created in DBConfig.xml and then Select any Database from the Database Drop Down and then select any table from Table drop down. You will see the columns as header and any row in the data grid. In case a table is system protected it will not display them. What if you provide any wrong Database name, nothing it will show you a message and continue working.

Main fight was to effectively utilize the SqlConnections. You will find below code self explaining which we used to solve the problem of connection pool.

C#
Edit|Remove
        private static SqlConnection getConn(DBConnections DBConn, String DBName) 
        { 
            if (DBConn == null) 
            { 
                DBConn = new DBConnections(); 
                DBConn.conn = new SqlConnection(DBConn.ConnString); 
                DBConn.conn.Open(); 
                DBConn.DBName = ""; 
            } 
            else if (DBConn.conn == null) 
            { 
                DBConn.conn = new SqlConnection(DBConn.ConnString); 
                DBConn.conn.Open(); 
            } 
            if (!DBConn.conn.Database.ToLower().Equals(DBName.ToLower())) 
            { 
                if (DBConn.conn.State != ConnectionState.Open) DBConn.conn.Open(); 
                DBConn.conn.ChangeDatabase(DBName); 
                DBConn.DBName = DBName; 
            } 
            return DBConn.conn; 
        } 
    public class DBConnections 
    { 
        public String ConnName { getset; } 
        public String DisplayName { getset; } 
        public String ConnString { getset; } 
        public SqlConnection conn { getset; } 
        public String DBName { getset; } 
    } 

At a place we required to get array of values out of Dictionary, we used Generics(for dictionary) and extension method to have that available to us.

 

C#
Edit|Remove
    public static class TPDictionary 
    { 
        public static TValue[] ToArray<TSource,TValue>(this Dictionary<TSource, TValue> dictionaryItem) 
        { 
            List<TValue> TVL = new List<TValue>(); 
            foreach (TValue TV in dictionaryItem.Values) 
            { 
                TVL.Add(TV); 
            } 
            return TVL.ToArray(); 
        } 
    } 
 

 

 

It simply checks in the DBConns to check if connection already exists for a database and returns the same after opening, in case it could not find the same it will create one and return the same after pushing it in the List.

Source Code Files

More Information

You need to change DB properties in DBConfig.xml prior to running the application.

Those using without buildingPlease open the DBConfig.xml file in bin folder and create individual entries for all the servers as below inside <DBs></DBs>

Those using after buildingPlease open the DBConfig.xml file at the root and create individual entries for all the servers as below inside <DBs></DBs>

 

XML
Edit|Remove
  <DB    <name>db1</name> 
    <displayname>My Database 1</displayname> 
    <conn>Data Source=.;Integrated Security=True;</conn> 
  </DB> 
 Assuming your sql server name is Dummy\Server then you need to feed Data Source=Dummy\Server;Integrated Security=True; as conn value. If you want to use SQL Server authentication then specify username and password as well and remove Integrated Security=True;.

PS: It must end with a semi colon (;) in order to work correctly.

Updates

Please rate this a 5 star (xxxxx) if you liked it. You can also consider donating us a small amount to help us build more free stuffs.

You may also like MongoDB Reference