Embedded Resource Queries or How to Manage SQL Code in Your .NET Projects

I developed a technique for managing SQL code in my .NET projects using the Embedded Resource “Build Action” back in the .NET 1.0 days that I still use to this day. I’ve been meaning to share this with the world for several years now and I’m finally getting around to it.

The benefits of this technique are many. Your SQL queries are contained in individually-editable, properly-formatted files in your .NET project instead of constructed as nasty concatenated strings in your code.

However, at run-time, you don’t have to worry about distributing extra files with your application, the SQL text is an embedded resource in your executable or class libary. You get the best of both worlds. You have source control diffs available but no additional distribution burden.

Before I go further, let me address the issue of stored procedures. Stored procedures have their place. In the past, I’ve maintained 2000+ line stored procedures. I’m ashamed to admit that I’ve even written some 2000+ line stored procedures back in the heyday of client server. Stored procedures (not 2000+ line long ones, surely) have their place and can be a great way to minimize network round trips. However, for purely select-oriented SQL, I think stored procedures are an unecessary administrative burden on most .NET projects. By using this technique, you never have to worry about your .net code being out of synch with the stored procedure.

Modern database servers cache query plans equally well for parameterized ad hoc queries as they do for stored procedures. As a result, unless you are doing some strange things, you will not find a measurable performance difference between using a stored procedure and the embedded resource technique I am about to demonstrate for select-oriented SQL.

Finally, let me say that if you work in a change management environment where it is easier to recompile stored procedures than it is to push out DLLs, then this technique may not be for you. Also, if you expect your query to be re-used as is by many different types of non .NET clients (for example, an excel spreadsheet or other stored procedures) then you may want to stick with a stored procedure. Keep in mind that you can easily re-use SQL stored as embedded resources in .NET DLLs among different .NET projects.

Many times I find myself in a query tool such as Query Analyzer, SSMS Query Editor, or Oracle SQL Developer iteratively developing a SQL query that returns a single recordset of results. Once I’ve got the query working reasonably, I create a new text object in under a folder named SqlQueries in Visual Studio in my project, rename it to something.sql and paste in the SQL from the query tool unaltered. I then comment out the section at the top of the query that declares the parameter variables (if I’m using SQL server). Notice that I don’t delete them. This allows me to easily run the query in a query tool if I need to refine or debug the query.

Once the file is in the project, you have to set the build action to embedded resource as shown below:

Setting Build Action to Embedded Resource

We need a class that helps us conveniently access our embedded resources. I’ve always used a class with static methods like this one:

public class EmbeddedResource {
	private EmbeddedResource() {
	}

	public static StreamReader GetStream(System.Reflection.Assembly assembly, string name) {
		foreach (string resName in assembly.GetManifestResourceNames()) {
			if (resName.EndsWith(name)) {
				return new System.IO.StreamReader(assembly.GetManifestResourceStream(resName));
			}
		}
		return null;
	}

	public static string GetString(System.Reflection.Assembly assembly, string name) {
		System.IO.StreamReader sr = EmbeddedResource.GetStream(assembly, name);
		string data = sr.ReadToEnd();
		sr.Close();
		return data;
	}

	public static string GetString(string name)
	{
		return EmbeddedResource.GetString(typeof(EmbeddedResource).Assembly, name);
	}
}

If I wanted to retreive some embedded sql in a file named SelectOrderWithExpensiveItemInfo.sql under a project directory directory named SqlQueries as a string to a variable named sql I would do this:

string sql = EmbeddedResource.GetString("SqlQueries.SelectOrderWithExpensiveItemInfo.sql");
SqlCommand cmd = new SqlCommand(sql);

You may be wondering about the for each loop inside EmbeddedResource.GetStream. Initially, I specified the whole resource name which included what I assumed was the assembly name at the beginning of the resource specifier. Unfortunately, the default namespace from project properties is what is actually used (at least when I first wrote this way back when). I couldn’t find a reliable way to retreive the default namespace so I added the search logic. Also keep in mind that you can only use GetString without passing the assembly in if your code is executing in the same assembly in which the embedded resource is housed.

That’s all there is to it. Here’s a fully-functional demo solution showing this technique in action using the AdventureWorks database. Enjoy. I hope that you find embedded resource SQL queries as useful as I have.

6 Responses to “Embedded Resource Queries or How to Manage SQL Code in Your .NET Projects”

  1. Chris Rogers Says:

    I had the inspiration to do this exact thing with my queries and was prepared to roll up my sleeves and bang out the embedded resources plumbing. Then I did a quick search and found this post, which was everything I needed. Though it’s not much code, it still saved me time and hassle — and it validated the technique. Thanks, John!

  2. Stored Procedures Versus LINQ to SQL for Dynamic WHERE Clauses « John Opincar’s Blue Corner Says:

    […] single SELECT queries with large numbers of WHERE and/or ORDER BY options. In fact, I favor code-based solutions over stored procedures unless performance dictates otherwise (which, in practice, is not very […]

  3. Richard Briggs Says:

    John,

    Excellent !

    I was building a similar embedded SQL resource solution for myself this evening, and Google searching came accross your page.

    I have an Application that will have hundreds of sites with their own sql databases. I came up with the same solution/idea as I have been struggling to imagine how I will ensure each version of the app and its stored procedures will stay in sync, and how one database could support differnt versions of the App.

    I have now REMOVED my application specific stored procedures and functions entirely replacing with this techinique.

    Notably I am using Visual Studio 2008, and this allows me code up and test files with SQL file extension within VS just like Query Analyser.

    I have read quite a few articles in last day or so as regards the stored procedure / dynamic sql/ embedded DAL, all very interesting., and made me really question he use of stored procedures.

    I used to be very much in favour of stored procedures, mainly beacuse my work involves hefty complex queries, too much to embed in concatinated text strings.

    With this technique Stored procs & functions have lost their shine for me – which considering I been coding them for near 15 years has to be significant! It also means I don’t need to worry about other developers making dependancies (or mesing about with!) my SQL.

    This technique is also a much easier transition for me rather than moving to MS LINQ.

    Cheers
    Richard Briggs
    Software Engineer
    UK

  4. darrinperry Says:

    Nice Post. This saved me heaps of time and made my SQL Queries more manageable.
    I too have stopped using stored procedures where possible.

    Well Done!

  5. Marcos Freire Says:

    How am I supposed to read an Embedded Resource Query by passing a parameter?

  6. Dustin Joines Says:

    just what I was looking for!


Leave a reply to Marcos Freire Cancel reply