Verbatim String Literal
As a novice C# user, I was recently happy to discover verbatim string literals, which are indicated by the presence of the @-symbol just to the left of
Here's the skinny; when you want to have a large block of text (SQL or XML markup, for example) in your C# code, you can have it formatted exactly the way it is formatted in your SQL or XML editor. This allows for excellent readability and rapid switching between your respective editors and your C# code page. Here is an example; the below SQL equally well in a C# page and an SSMS query editor. It has really sped up my debugging of in-line SQL. As far as I know, VB.NET doesn't have an equivalent feature.
DataSet ds = MyClass.GetData(@"
SELECT 0 AS Field1, '<>' AS Field2
UNION ALL
SELECT
Field1,
Field2
FROM
A_Table
WHERE
[Year] = udfFiscalYear()
ORDER BY
Field2
", "A_Table");
2 Comments:
Not to detract from your excellent find, but I have struggled with SQL in the same manner, using VB and a query editor for Oracle. The tool I used was called "Toad for Oracle" and it could format SQL in the same manner you described, in only an instant. It could also instantly convert the SQL into a VB compatible text string.
So I didn't need to store the SQL as formatted text in the source code since I could easily convert it between the two environments so easily.
Another way to store SQL is in a table. I created a "memo" type field where I could store the SQL and when I wanted to execute it in code, all I had to do was look up the SQL in the table. Then, if I needed to modify the SQL (when the data structure changed) I only needed to fix the SQL in the table, and I didn't need to recompile the VB code.
Instead of hard coded SQL or storing it in a table (!) you could just use a stored procedure.
I don't mean to be rude but I don't think you should be blogging about writing code when you are at such a basic level.
Post a Comment
<< Home