When it comes to storing SQL in code, most people use strings:
Dim strSql As String = "Select Foo, Bar From FooBarTable Where Something = @Something"
The problem is, when you have very long SQL strings, reading them in code becomes difficult. You can usually split the strings up like this:
Dim strSql As String = "Select Foo, Bar From FooBarTable " & _
"Where Something = @Something"
This causes a number of problems:
- You can't copy it to or from SSMS
- It uses an inefficient way (
&
) of concatenating the string (not that it really matters since your DB is orders of magnitude slower than the string class)
So why not store long SQL strings as a System.XML.Linq.XElement
type?
Dim SQL = <SQL>
Select Foo, Bar From FooBarTable
Where Something = @Something
</SQL>
You can call it in code by using SQL.Value
.
With this method you can preserve line breaks and formatting to make it both easier to read and easier to copy around.
My question is, basically, did I stumble across a clever way to store multi-line SQL strings in code, or am I totally missing something here? It seems to work in my code, but I'm not sure how efficient it is.
Any thoughts?