PatrickVerbeeten.com
Search this site Powered by Google
Something completely diferent my Photo album

Embedded Sql, LINQ like behaviour

When ever you create an application which uses a database. There certainly are variatons but you have basically two option when it comes to how you have de SQL-Statements. You can choose to type the SQL-statements in the source code or you can create stored procedures. Both of these have their own advantages and disadavatanges.
In-code statements ensure that when te application is deployed, no stored procedures have to be updated. Stored procedures on the other hand offer better speed and reusablility.

To allow stored procedures to be created, maximize easy of deployment I created the this. The .sql files can be edited in either Visual Studio or the SQL Management studio with full support of the syntax checkers and code-Highlighting. This class does have one other main feature it makes each declared procedure available as a strong type .NET class to access the parameters.

Download

There are two zip files which contain the binaries and the source code for this. The files contain a readme file which explains installation and the basic usage.

Zip with the Sourcecode the source code and Zip with the Sourcecode the binaries

The source is written in C# but embedded sql van generate code for any .Net language which has implemented a Code Dom i.e. C#, VB.Net and J#

What does it do?

Consider the procedure

CREATE PROCEDURE MyProcedure
@param1 int
AS
SELECT @param1

Calling this procedure would normally require you the create a new command object, pass in the procedure name, add the parameter (with value) and execute the command. Then to manage the procedure it self you would either create it in the database or create a file with the create statement.

When using embedded sql you can add the procedure declaration a normal .sql file containing a stored procedure declaration and ASP.Net or Visual Studio can than parse this file into a .Net class which exposes each of the parameters as typed properties (In the example above this would be a property named param1 with type SqlInt32). Now you can manage the procedure in the same place it is used and if you open it in for instance Sql management studio with all the benefits of syntax highlighting and validation.

Can't use stored procedures?

If you can't you stored procedures for any reason you can just as easily use embedded sql to eliviate some issues, just declare the procedure as temporary that is prefix the name with a #-symbol. The generator will use the header of the statement just to learn about the parameters and at execution time it will pass the parameters and the statement body (everything after the AS keyword) to the server.

What is supported

This bit is a work in progress, but all the basic functionality works perfectly.

Supported Not Supported
Schema names Three or Four part names
Input/Output parameters Default values for parameters

T-Sql with Sql server 2000 and up

 

Supported languages: Any language supported by the Code Dom

Verified: C# and VB.Net

 

There is bound to be more things which don't work yet, just let me known and I will see want I can do about it.

 


Listings

Embedded Sql is listed on
HotScripts.com

Tuesday, October 16, 2007

©2010 Patrick Verbeeten, Disclaimer, Privacy Policy