Click here to Skip to main content
5,788,961 members and growing! (20,723 online)
Email Password   helpLost your password?
Languages » C# » Utilities     Intermediate License: The Code Project Open License (CPOL)

Class Generator for SQL Server Stored Procedures

By Stefano Manni

Given a stored procedure, generate a C# class to use it
C#, Dev

Posted: 17 Sep 2008
Updated: 17 Sep 2008
Views: 2,661
Bookmarked: 15 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 1.62 Rating: 3.40 out of 5
1 vote, 33.3%
1
0 votes, 0.0%
2
1 vote, 33.3%
3
0 votes, 0.0%
4
1 vote, 33.3%
5

Introduction

This is a simple program that, given a connection string to SQL Server, and a list of stored procedure names, generates a strong typed class for each stored procedure to invoke it.

Background

No background is necessary.

Using the Code

The program usage is simple. First, you must create a *.txt configuration file. It must be as follows:

###########################################################
# All fields are mandatory, exception for NAMESPACE       # 
###########################################################


#sql server connection string
CONNECTION_STRING=Data Source=.\SQLEXPRESS;
	Initial Catalog=Northwind;Integrated Security=True

#output file name
FILE_NAME=c:\GeneratedStored.cs

#list of stored procedures, comma (,) separated. e.g. CustOrdersDetail,SalesByCategory
#if you want to generate class for ALL stored, set * (ASTERIX)
STORED_LIST_COMMA_SEPARED=CustOrderHist,
	CustOrdersDetail,CustOrdersOrders,SalesByCategory

#namespace (not mandatory)
NAMESPACE=Northwind.StoredClasses

Then launch the program from the command line, providing the configuration file path as the first parameter.

Here's a snippet of a generated class for stored procedure CustOrdersDetail of the Northwind database:

public class SP_CustOrdersDetail
{   
    public static string STORED_NAME = "CustOrdersDetail";
    public static string P_OrderID = "@OrderID";
    public int ReturnValue;
    public Int32? IN_OrderID { ... }
    public DataSet execute(SqlConnection conn, SqlTransaction t) { .. }
    public DataSet execute(String connectionString) { ..}
    public void executeNonQuery(SqlConnection conn, SqlTransaction t) { .. }
    public void executeNonQuery(String connectionString) { ...}
}

Here's an example of a generated class for stored procedure CustOrdersDetail of the Northwind database.

You have a strong typed property for each database parameter, prefixed from "IN_" or "INOUT_" depending on the parameter direction.

To set a parameter to DB NULL, set the related property to null.

If you want that a parameter not be sent to stored, simply don't set related property. (You must ensure that the related stored parameter has a default value, otherwise you will have a runtime error.)

Once the parameters are filled, simply call execute or executeNonQuery methods. After this call, out parameter will be available.

Points of Interest

I hope this simple project helps you to write simple code and in a smaller amount of time.

History

  • 18th September, 2008: Now you can set * to stored list, generating a class for each procedure in the database.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Stefano Manni



Occupation: Web Developer
Location: Italy Italy

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralNice work, really cool!memberwasco773:41 18 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Sep 2008
Editor: Deeksha Shenoy
Copyright 2008 by Stefano Manni
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project