|
Location: Desktop development - C# License: The Microsoft Public License (Ms-PL) Call Your Stored Procedures With EasePosted by Wong Shao VoonThis article presents a library which enables ADO.NET developer to call SQL Server stored procedures with mininal effort. |
Skill: BeginnerPosted: 10/07/2011Views: 189Rating: 0.00 /5Popularity: 0.00 |
| Sign Up to vote for this article |
Most ADO.NET developers do not relish the mindless drudgery of writing plain ADO.NET code to call stored procedures. Writing the stored procedure often involves repetitive code but they often varies enough to warrant a rewrite for each new stored procedure which our application must call. This article presents a library called Stored Procedure Caller, to enable developers to call SQL Server stored procedures with mininal effort.
For purpose of this article and the next example, I'll use this table, Employee and the following stored procedure, sp_InsertEmp.
This is the typical plain ADO.NET code to call this stored procedure, sp_InsertEmp.
The above ADO.NET code takes about 75 lines of code. How many lines of code does Stored Procedure Caller take to call sp_InsertEmp?
Let Stored Procedure Caller perform some voodoo magic for you!
The Stored Procedure Caller takes 34 lines of code to do the same call, which is about half of the original code. We can see from the code:
SPSignature takes in a string which contains the sp_InsertEmp signature in its constructor and parse it into parameters. SPCaller takes in
SPSignature object in its constructor. For performance reason, you should keep SPSignature object around because it is expensive to parse the
signature everytime you need to call your stored procedure. You can save SPSignature parsed information into a XML file and load that instead of
parsing the signature string; Its Load and Save method allows you to do that. XML parsing and saving is done using the
Elmax library.
I guess everyone, by now, is very curious about the SPCaller.CallVoidProc. I'll explain this right away.
Below is the declaration of the CallVoidProc method and its sibling methods.
CallVoidProc is used for calling stored procedure which return nothing. Whereas CallIntProc returns integer from stored procedure
and CallDataSetProc is for stored procedure that returns table(s).
You see that InsertEmployee method takes in ref integer parameter called ID. I wrapped this argument in a Output object before passing to
CallVoidProc. That's because CallVoidProc takes in a variable number of Object objects for the stored procedure parameters; I am not allowed
to pass in a ref argument to the CallVoidProc. As you might be wondering why I did not make Output class a generic class. The reason is
because there is no way to cast the object back to a generic Output class inside CallVoidProc. If the stored procedure has the InputOutput
parameter, then you should use a InputOutput class to wrap your variable. But right now, InputOutput and Output implementation has virtually
no difference: Output is an empty class which inherits from InputOutput class.
For those readers who is interested in the CallVoidProc code, I list it below. However, I refrain from listing the CallIntProc and
CallDataSetProc because they are very similar with some minor differences. For your information, these 3 methods compares the supplied
stored procedure name with the signature name and also checks the .NET parameter types with the SqlType parameter types in the signature;
They can only call stored procedures with parameters' SqlType which can be mapped to a corresponding .NET type.
For some readers who may be concerned about the overhead of Stored Procedure Caller and would like to use hand coded ADO.NET to call their stored procedures. I am happy to let you know Stored Procedure Caller lets you have your cake and eat it. Stored Procedure Caller can auto-generate ADO.NET code for you. However, it can only do that in runtime. These are the methods which allows us to auto-generate code.
This is how I'll modify the original Stored Procedure Caller to generate the code.
Here is the generated code.
You may need to edit your connection string variable name and your parameter names to match the generated code.
You will notice the code generated for the Children nullable parameter is wrong. It should include a check for null.
The reason for this error. The Object object wraps integer, instead of the nullable integer. The code below prints
"System.Int32" both for the integer and nullable integer type. In case the reader is not aware of, ? is a shorthand for
declaring a nullable type.
To fix the error, the developer just need to pass null for all nullable parameters and the code generated, will be correct.
Using Stored Procedure Caller, developer are freed with more time to surf facebook and do tweeting to focus on reviewing, refining and refactoring their code elsewhere to higher quality. Stored Procedure Caller is hosted at Codeplex. Feel free to report any problems, you encounter to Codeplex. Lastly, I would love to hear your feedback on things I am doing right and the things I am doing wrong.
Thank you for reading!
This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)
| Wong Shao Voon
| I guess I'll write here what I does in my free time, than to write an accolade of skills which I currently possess. I believe the things I does in my free time, say more about me. When I am not working, I like to watch Japanese anime. I am also writing some movie script, hoping to see my own movie on the big screen one day. I like to jog because it makes me feel good, having done something meaningful in the morning before the day starts. I also writes articles for IntelliProject; I have a few ideas to write about but never get around writing because of hectic schedule. Location: |
Sign up to post message on the article message board!