Using SMO For Dynamically Creating Stored Procedure and Database Tables in C#.NET

According to me, Giving a deadly desired storm to your brain might yield to an ‘invention.’

What is SMO?

Server Management Object (SMO) is Microsoft® .NET Framework library of objects that let you manage one or more database servers. SMO can be used to examine a database’s objects and to perform a variety of administrative tasks like backing up and restoring databases, and issuing Data Definition Language (DDL) commands. Using SQL SMO you can also connect to SQL Server, iterate through a collection of database objects and perform a variety of tasks against them.

Let me demonstrate the use of SMO for dynamically creating of Stored Procedures and Database Tables.

For start up, let us first reference the appropriate assemblies that use SMO.

The two main DLLs you must reference are:

  1. Microsoft.SqlServer.Smo.dll
  2. Microsoft.SqlServer.ConnectionInfo.dll

You will also need the following assembly for creating indexes for primary key:

Microsoft.SqlServer.SqlEnum.dll

After adding the above assemblies let’s begin Dynamic Creation of Stored Procedure and Database Tables.

Dynamically Creating Database Tables

Below is a simple Code that creates the database table known as tblEmployee in my personal database Employee with a primary key and identity on the “ID” column.

//Establish Conection

SqlConnection conn = new SqlConnection

(“Server=;DATABASE=;uid=;pwd=”);

//Create Server instance

Server server = new Server(new ServerConnection(conn));

//Create Database Instance from existing databases

Database database = server.Databases[“Employee”];

//Create Table

Microsoft.SqlServer.Management.Smo.Table table = new

Microsoft.SqlServer.Management.Smo.Table(database, “tblEmployee”);

Comments are closed.

Freelance Web Designer | WordPress Developer | Hong Kong