Click here to Skip to main content
Click here to Skip to main content

SQL Stored Procedure Wrapper & Typed DataSet Generator for .NET

By , 7 Dec 2002
 

What is this?

DBHelper is a small tool that will generate either a source file or a compiled assembly for selected stored procedures on your SQL server. It uses the CodeDom namespace to output the code. Currently it supports C# and VB.NET code generation (and other language support is likely in the future), but the generated assembly is usable from any .NET language. Also included in version 1.5 is the ability to compile a complete assembly that includes a typed DataSet based on your SQL stored procedures output.

Basically, this is an all-in-one tool for wrapping a SQL database. Great for existing databases, and for rapidly changing databases. E.g. changes gets made to stored procedure's parameters or the output of stored procedures. No problem for DBHelper, just a few clicks and you a have all the methods remapped/DataSet regenerated. All you have to do it adjust your corresponding .NET methods.

NOTE: DBNulls are still not supported. I'm still looking for a way. The alternative is to still generate the source file and the XSD file, and change all the method parameters from their strong type to System.Object. That's all! Hopefully there wont be too many!

New information (version 1.5+)

As of version 1.4, the package comes as a tool rather than a library. Please select the SQL provider (no others will work and I have NO plans to support it). If you tick Create, it will create a corresponding method, and CreateStrong will generate an XSD for strongly typed DataSet/Tables.

Also note VS.NET is NO longer required for typed DataSet generation and compilation (overlooked a static method).

Version 1.6 includes plenty code optimization. Also, typed datasets now get created based on return values. The is no more option to select which ones you want. I have tested it successfully on the MASTER database.

Why?

Some people might ask "Why?". The answer is sweet. Take the following for example:

Every time you even want to execute a simple stored procedure you need something like the following:

SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = "ChangeEmail";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.BigInt).Value = id_param;
cmd.Parameters.Add("@oldemail", SqlDbType.VarChar).Value = oldemail_param;
cmd.Parameters.Add("@newemail", SqlDbType.VarChar).Value = newemail_param;
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();

Now that's only 3 parameters, how about some nasty UPDATE procedures with 10+ parameters? That can be a real time waster, especially doing all the type mapping by hand. On the other hand you could always go for DataSets, and let VS.NET do the work. My personal liking is to use DataSets only for SELECT queries and I usually do all the other statements via SqlCommand's as above. The library will generate all the required parameters and present you with a strongly typed wrapper method for the stored procedure.

UPDATE: I have now updated the library to return a DataTable of the output. For example:

public static DataTable ChangeEmail(SqlConnection connection
    DataTable table, long id_param, 
    string oldemail_param, string newemail_param)

The code from the former example is automatically inserted into a class of choice. Now that looks a lot better, and will probably save you countless hours.

How does it work?

These methods are a bit redundant as of version 1.4 as it run as a tool rather than a library. Firstly your database will be queried for stored procedures. After that we get the name and type of the parameters for each procedure. With the help of CodeDom we create a new class to output to a source file or an assembly.

Constructor

  • SP(SqlConnection conn, CodeDomProvider cp);

    Just a reference to a predefined SqlConnection. See example for CodeDomProvider usage.

    E.g. SP sp = new SP(myConnection, new CSharpCodeProvider);

Properties

  • public SPCollection Names;

    Returns a strongly typed string collection of all the stored procedure names. This allows you to remove the ones you don't need methods for (as in the UI example).

Methods

  • public CompilerResults Compile(string ns, string classname, string asmname);

    Compiles to an assembly (asmname) using values from Names property to generate. Returns results (primarily for debugging).

  • public CompilerResults Compile(string[] storedprocs, string ns, string classname, string asmname);

    Compiles to an assembly (asmname) with own defined string array of stored procedures to generate. Returns results (primarily for debugging).

  • public void CreateFile(string[] storedprocs, string ns, string classname, string filename);

    Same as compiler, but will output source file.

  • public void CreateFile(string ns, string classname, string filename);

    Same as compiler, but will output source file.

Notes

I use a very, very simple mapping from SqlDbType to Type and DBNull values are not supported either in this release.

For the test app to work you will need to change the SqlConnection to your linking.

Free plug

Kristopher Johnson - Typed Collection Class Generator for creating all the collections and dictionaries in DBHelper (although the CodeDom code was not looked at).

Conclusion

This was really my first successful use of CodeDom and it seems really powerful, especially if you use it correctly as output to any language should be possible. As usual, bugs, problems, praise, suggestions, gifts can be added below.

UPDATE: I have created support for VB.NET code as well.

Cheers :)

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

leppie
Software Developer
South Africa South Africa
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberDon_Hard11 Jul '10 - 20:45 
Questiondatabase Entry (SQL 2000)memberSapan Jaiswal6 Mar '08 - 9:13 
QuestionHow to use dataset in Sql Server to retrive the datamemberJanvi3 Mar '08 - 19:50 
GeneralVS.Net 2005 issues & Sample implementationmemberjcnovoa20 Dec '05 - 10:53 
GeneralSP returning multiple result setsmemberGoTech6 Dec '05 - 4:44 
GeneralA simple utilitymemberchris Liang30 Aug '05 - 9:06 
GeneralRe: A simple utility - a free one!memberThomas Schittli28 Sep '05 - 3:10 
GeneralBlows up in my eye when I run the demo!memberCyberhacker29 Jun '05 - 6:38 
Questionhow to use this ??memberwebber12345628 Jun '05 - 8:42 
GeneralSQL Errors are not reported,membermostafaa10 Feb '05 - 10:38 
GeneralSweeet...memberJoshCrosby23 Dec '04 - 5:14 
GeneralRe: Sweeet...memberleppie23 Dec '04 - 5:49 
GeneralRe: Sweeet...memberJoshCrosby23 Dec '04 - 13:54 
GeneralCannot Get It To Work With Oracle 9imemberMark Kamoski25 May '04 - 6:04 
FYI, I cannot get this to work with Oracle 9i.
 
Here is what happens.
 
I run the project, it prompts me for database information, I fill in the database dialog, I press "Test Connection", it says "OK", then the Generator program complains that I must "choose a database too", I go back to the database info dialog, I use the same UID and PWD and I try to list the initial catalog to use and it says "invalid parameter", and I am stuck.
 
It does the same thing for OLEDB and Oracle provider selection.
 
(Note that I can connect to this Oracle instance using other programs such as TOAD, MyGeneration, SQL Navigator. When I supply the same credentials, those programs can find the database information just fine.)
 
Any ideas?
 
Please advise.
 
Thank you.
 
--Mark

GeneralRe: Cannot Get It To Work With Oracle 9imemberleppie25 May '04 - 9:45 
QuestionWill this work for Oracle 9i?memberMark Kamoski22 May '04 - 12:41 
GeneralJust came across thismemberMichael P Butler12 Apr '04 - 0:20 
QuestionHow to invoke wrapped procedures?memberIryx29 Sep '03 - 8:48 
GeneralException running example programmemberversteijn20 Jul '03 - 5:14 
GeneralRe: Exception running example programmemberleppie20 Jul '03 - 6:40 
GeneralRe: Exception running example programmemberversteijn22 Jul '03 - 8:52 
GeneralRe: Exception running example programmemberleppie22 Jul '03 - 9:10 
GeneralRe: Exception running example programmemberversteijn29 Jul '03 - 8:10 
GeneralRe: Exception running example programmemberleppie29 Jul '03 - 9:40 
GeneralRe: Exception running example programmemberJonathan de Halleux29 Aug '03 - 0:07 
GeneralRe: Exception running example programmemberquincykong23 May '04 - 22:46 
GeneralRe: Exception running example programmemberMrFishy21 Jan '05 - 1:11 
GeneralUsing with IDENTITY FieldmemberDaniel Bright25 May '03 - 9:55 
GeneralRe: Using with IDENTITY Fieldmemberleppie25 May '03 - 10:12 
GeneralRe: Using with IDENTITY FieldmemberDaniel Bright25 May '03 - 14:21 
Generalissue with image data type parametermemberandy_net18 May '03 - 3:23 
GeneralRe: issue with image data type parametermemberleppie8 May '03 - 7:36 
Generalsql server 7.0 fixmemberVorn Mom14 Apr '03 - 12:22 
GeneralLeppiememberPaul Evans30 Mar '03 - 23:25 
GeneralAdded output and return parameterssussAndreas Gratz20 Mar '03 - 2:21 
Generalbug in xsdmemberuarndt14 Mar '03 - 3:18 
GeneralRe: bug in xsdmemberleppie14 Mar '03 - 20:39 
QuestionHow to Use??membershobapond22 Jan '03 - 14:57 
AnswerRe: How to Use??memberbwical26 Feb '03 - 7:20 
GeneralRe: How to Use??memberleppie26 Feb '03 - 7:32 
AnswerRe: How to Use??memberleppie26 Feb '03 - 7:29 
GeneralSimilar project...memberMauricio Ritter19 Dec '02 - 0:01 
GeneralRe: Similar project...memberleppie19 Dec '02 - 6:13 
GeneralExecute Only SP'smemberOldRob26 Nov '02 - 7:28 
GeneralRe: Execute Only SP'smemberleppie26 Nov '02 - 10:43 
GeneralRe: Execute Only SP'smemberleppie26 Nov '02 - 14:17 
GeneralRe: Execute Only SP'smemberOldRob27 Nov '02 - 7:42 
GeneralRe: Execute Only SP'smemberleppie27 Nov '02 - 8:22 
Generalmistakemembervjedlicka24 Nov '02 - 21:33 
GeneralRe: mistakememberleppie25 Nov '02 - 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 8 Dec 2002
Article Copyright 2002 by leppie
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid