This article will give you an idea of how easily you can generate the SQL server database object script using C#. NET.
This article will be helpful for developers who want to generate the SQL Server database objects script like stored procedure, views, etc.
Using the Code
It’s a very easy way... I hope that you will agree with me that the flexibility of Microsoft products to integrate with your custom application is outstanding. Microsoft SQL Server provides a special stored procedure
"sp_helptext", This stored procedure allows you to get the description of a given object.
Here I write a function to get the SQL script from a given object. You just need to provide three parameters:
- Connection string
- Object name
- Object type (I used this to determine whether it's a Table or not)
exec sp_helptext 'object'
A sample code example is given below.
public string GetScript(string strConnectionString
, string strObject
, int ObjType)
string strScript = null;
int intCounter = 0;
if (ObjType != 0)
ObjSqlConnection = new SqlConnection(strConnectionString.Trim());
ObjDataSet = new DataSet();
ObjSqlCommand = new SqlCommand("exec sp_helptext
[" + strObject + "]", ObjSqlConnection);
ObjSqlDataAdapter = new SqlDataAdapter();
ObjSqlDataAdapter.SelectCommand = ObjSqlCommand;
foreach (DataRow ObjDataRow in ObjDataSet.Tables.Rows)
strScript += Convert.ToString(ObjDataSet.Tables.Rows[intCounter]);
catch (Exception ex)
strScript = ex.Message.ToString();
ObjSqlDataAdapter = null;
ObjSqlCommand = null;
ObjSqlConnection = null;
Points of Interest
The stored procedure
sp_helptext will not allow to give you any table description.
I hope that this article will be helpful to you. Enjoy!
- 8th September, 2009: Initial post
He is the founder & CEO of MNH Technologies and working for urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc. He has over ten years of professional experiences in design and developing Client-Server, Multi-Tier, Database, Web based business software solutions, Enterprise Applications, API, WebAPI, Google Analytics implementation, Add-In, Documentation & Technical Writing etc for Windows / Mac using Microsoft SQL Server, Oracle, MySql, PS, C#, VB.NET, ASP.NET, PHP, RoR, Visual Basic etc. He has also more than two years experience in Mobile-VAS (Platform Development).
He worked for various software development & technology consulting. His core focus on technologies to create dynamic data-driven systems that add value to your business and dynamic technology consulting that builds advanced solutions for the industries across the various vertices.
He also work as a Solution Architect at Dhrupadi Techno Consortium Limited (DTCL) and responsible for analyzing business requirements and offered optimum solutions (multiple options), which would address all current requirements, provide flexibility for future growth and allow smooth transition between old system and new system.
He graduated with honors from The University of Asia Pacific, in Computer Science and Engineering. He was awarded as “Most Valuable Professional” (MVP) at 2010 and 2011 by CodeProject.com and also selected as a Mentor of CodeProject.com
Specialties: Software Development Management, System Integration, Data Warehouse Architecture, Virtualization.