ARTICLE

Dynamically Getting Tables Collection From SQL Server Using C#

Posted by Lajapathy Arun Articles | C# Language April 25, 2012
In this article we are going to see, how to get tables from a database dynamically using C#.
Reader Level:

In this article we are going to see how to get tables from a database dynamically using C#.

Step 1: Used Namespaces:


using
System.Web.Configuration;
using
System.Data.SqlClient;
using
Microsoft.SqlServer.Management.Common;
using
Microsoft.SqlServer.Management.Smo;


Step 2: Referenced DLL:

Image1.jpg

Step 3: Connection String:

<add name="<KEY>" connectionString="Data Source=<SERVERNAME>;Initial Catalog=<DATABASE>;Integrated Security=True" />

Step 4: Used to get Connection string from Web.config file

public static string GetConnectionStringFromWebConfigByName(string name)
{

    return
WebConfigurationManager.ConnectionStrings[name].ConnectionString;
}


Step 5: Usage:

SQLTableDatabaseClass tables =  SQLTableDatabaseClass.
GetTablesFromDatabase (connectionString,"databaseName");

Here you can get the tables of database as List.

Table table =  SQLTableDatabaseClass.
SingleTable(connectionString,"databaseName");

Here you can get a single table by its name.

Step 6: Creating Connection string for server connection:

public static SqlConnection Connecection(string connectionString)
{
    SqlConnection con = new SqlConnection(connectionString);
    return con;

}


Step 7: Creating Server connection using Connection string:

public static ServerConnection GetServerConnection(string connectionString)
{
    ServerConnection serverCon = new ServerConnection(Connecection(connectionString));
    serverCon.Connect();
    return serverCon;

}


Step 8: Creating Server Object:

public static Server GetServer(string connectionString)
{
    Server server = new Server(GetServerConnection(connectionString));
    return server;

}

Step 9: Getting Tables from Database in the SQL Server:

public static TableCollection GetTablesFromDatabase(string connectionString, string databaseName)
{
    Database db = SinglDatabase(connectionString, databaseName);
    return db.Tables;

}


Step 10: Getting a single table from the database:

public static Table SingleTable(string connectionString, string databaseName, string tableName)
{
    TableCollection tableCol = GetTablesFromDatabase(connectionString, databaseName);
    return tableCol[tableName];
}

Step 11: Getting a specific database from the collection of databases in the SQL Server:

public static Database SinglDatabase(string connectionString, string databaseName)
{
    return GetServer(connectionString).Databases[databaseName];
}


COPY & PASTE Code Snippet:

using System.Data.SqlClient;
using
Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace ApplicationClassLibrary
{
    class SQLTableDatabaseClass
    {
        #region Database
        public static TableCollection GetTablesFromDatabase(string connectionString, string databaseName)
        {
            Database db = SinglDatabase(connectionString, databaseName);
            return db.Tables;
        }
        public static Table SingleTable(string connectionString, string databaseName, string tableName)
        {
            TableCollection tableCol = GetTablesFromDatabase(connectionString, databaseName);
            return tableCol[tableName];
        }
        public static Database SinglDatabase(string connectionString, string databaseName)
        {
            return GetServer(connectionString).Databases[databaseName];
        }
        #endregion
        public static Server GetServer(string connectionString)
        {
            Server server = new Server(GetServerConnection(connectionString));
            return server;
        }
        public static ServerConnection GetServerConnection(string connectionString)
        {
            ServerConnection serverCon = new ServerConnection(Connecection(connectionString));
            serverCon.Connect();
            return serverCon;
        }
        public static ServerConnection GetServerConnectionByLogin(bool isWindows, string serverName)
        {
            ServerConnection serverCon = new ServerConnection();
            serverCon.LoginSecure = isWindows;
            serverCon.ServerInstance = serverName;
            serverCon.Connect();
           return serverCon;
        }
        public static SqlConnection Connecection(string connectionString)
        {
            SqlConnection con = new SqlConnection(connectionString);
            return con;
        }
    }
}
 
 
Thanks for reading this article. Have a nice day. 

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

HI Vipendra ,Please google it because i am not aware of it. Here me using sql server DLL, for this we need Oracle DLL to perform.

Posted by Lajapathy Arun Apr 26, 2012

Arjun: Means you need to create Column dynamically in the table using sql server DLL?.

Posted by Lajapathy Arun Apr 26, 2012

Thanks, How i can add columns to table in SQL server using Asp.net C#?

Posted by Arjun Panwar Apr 26, 2012

Thank you for providing good knowledge about how to Dynamically Getting Tables Collection From SQL Server Using C#. I want to know how Dynamically Getting Tables Collection From Oracle Using C#

Posted by Vipendra Verma Apr 25, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts