When I have to put SQL strings in code I like to it become like that (example):
VB.NET " Select " + vbCrLf + _ " Id, " + vbCrLf + _ " Name " + vbCrLF + _ " from " + vbCrLF + _ " Customers " + vbCrLF C# var vbCrLf = "\r\n"; var SQL = " Select " + vbCrLf + " Id, " + vbCrLf + " Name " + vbCrLF + " from " + vbCrLF + " Customers " + vbCrLF ;
That way have some advantages:
- Since each like gets an NewLine sequence, when I have to read SQL on a server trace I don't have to reformat to get an easy reading
- Because each line is surrounded in spaces, there's no risk of string concatenations generate an invalid SQL (something in the line of
select Id from Customerswhere Id =1)
- If I have to review it on an separated SQL application (SSMS, IBExpert, SQLDBX, etc) I can block select easily.
It's somewhat tedious to make it by hand. There's some plugin/extension that already do that? I looked on VS setting, but didn't find something like that.
EDIT: All examples are made-up. No actual code here and there's no need to preach me on parameterized queries either, as I already use them.