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
A highly experienced leader with successful track record of software development, product innovations, brand management and corporate communication etc. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by codeproject.com and also selected as a mentor of codeproject.com. Published over 100 technical articles in various software development resource sites (i.e., codeprojetc.com, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.
Over ten years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, development, services, data management and analytics, customer experience management, content services, digital analytics and optimization.
An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint. Believes in transparency, commitment and teamwork.
Expertise: Software/Solution Architect, SaaS platform base application, Large scale win32/web based business software solutions, enterprise applications, integration, etc.
Technologies/Tools: Microsoft.Net version 05/08/10/12, Microsoft SQL Server version 7/2K/05/08/12 , Oracle version 10/11, MySql version 5.1, 5.5, PS2, Visual C#, R, VB.NET, ASP.NET, PHP, API, MVC, WebAPI , Add-In Visual Basic etc.,. I have also more than two years’ of strong experience in mobile-VAS (platform development).
Points of Interest: Technology and research & development especially focused on business functionalities and social business areas as well, few stuff including:
1.R&D on new techniques as required to increase business revenue.
2.Urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc., using innovative technical solutions.
3.Research and innovative product development.