Using ad-hoc SQL queries in C# – Layout
Had a lot going on at work of late, and have been unable to keep up with posts here.
However, I feel this one deserved some attention, as the more i write about it, the more I can remember how it all works.
A lot of people will say not to use ad-hoc queries in c# applications. Yet, for QA testing, i think they’re useful. Do you really want to ask your DBA to write you a boat load of SP’s? Not the best use of a DBA’s time really.
So just a quick one to start with. Layout of SQL. Thankfully with QA tests, most SQL statements will usually be pretty short and sweet. But in case one gets created that’s longer than expected, it can get pretty messy keeping it all on one line.
So here’s what you do to resolve that…
As long as you use the @ syntax then you can have the SQL span multiple lines just fine.
For example
string sql = @"select colA, colB, colC from tableX inner join tableY on tableX.colA = tableY.colA where colB > 20;"