|
What's wrong with it? I mean, it's simple enough I'd put it on one line, but I don't see anything in principle wrong with putting it on several, and as C# doesn't have multi-line string constants, you have to write it as it is there. Edit: apparently @ strings will let you do multi-line constants.
Making code a stored procedure hides it away from the developer and makes it harder to see. Select queries should almost never be in one because it makes you go and look at the database to find out what the code is doing ... or, to put it another way, those queries are part of the business logic and should be in the code. But I have a somewhat old fashioned view of the database as essentially a minimally intelligent data store.
|
|
|
|
|
I like the database to take that strain: it's what it is there for, after all. And I never, ever put SQL in code: I either use a view or a stored procedure.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum
me, me, me
|
|
|
|
|
Oh yes, I have to agree. Prevents SQL injection attacks.
|
|
|
|
|
Also you can fake multiline strings (in VB.NET, say) with an inline XML document, of which the text is then converted to a string.
|
|
|
|
|
Yeah, this code is incredibly silly.
Anybody knows it should be private static readonly string SQL =
modified 15-Apr-13 19:24pm.
|
|
|
|
|
I don't know if you are being serious or if you are joking, after all, you are saying something intermediary... it is not an horror, but it is not right... it's strange.
|
|
|
|
|
Edited my post...
|
|
|
|
|
Indeed, not a candidate for StringBuilderhood. And I write it as
private const string SQL =
@"
SELECT ID
, NAME
, BIRTHDAY
FROM TABLE
WHERE NAME LIKE @PARAM
" ;
so it prints out nice in error messages [added>>] and I can very easily copy/paste it between a code file and SSMS or other SQL executor.
|
|
|
|
|
|
PIEBALDconsult wrote: And I write it as
So to be clear your code looks like the following?
And this format is 'better'?
namespace mystuff.otherStuff
{
public static class MyDbConstants
{
private const string SQL1 =
@"
SELECT ID
, NAME
, BIRTHDAY
FROM TABLE
WHERE NAME LIKE @PARAM
" ;
private const string SQL2 =
@"
SELECT ID
, NAME
, BIRTHDAY
FROM TABLE_OTHER
WHERE NAME LIKE @PARAM
" ;
private const string SQL3 =
@"
SELECT ID
, NAME
, BIRTHDAY
FROM TABLE_OTHER2
WHERE NAME LIKE @PARAM
" ;
|
|
|
|
|
No, I don't use const s for SQL.
|
|
|
|
|
PIEBALDconsult wrote:
No, I don't use const s for
SQL.
Then we have a term definition problem because I responded to what you said...
"And I [PIEBALDconsult] write it as ...private const string SQL"
|
|
|
|
|
|
PIEBALDconsult wrote: I meant "that".
No idea what that means.
|
|
|
|
|
Looks pretty damned neat to me. I always write out my SQL with each identifier or keyword on its own line. Much easier to read and diagnose.
|
|
|
|
|
Brady Kelly wrote: Looks pretty damned neat to me.
I don't know what you are referring to.
I have no problem with multi-line SQL constructs.
|
|
|
|
|
PIEBALDconsult wrote: so it prints out nice in error messages. If that is where you want it printed out nicely, you could use:
...= "\nSELECT ID\n, NAME\n, BIRTHDAY\nFROM TABLE\nWHERE NAME LIKE @PARAM\n";
The thing I don't like about @"... is that intellesense puts in indents you don't want, while most of the time, intellesense is so handy, I don't like working without it. Of course, you might be one of those people who like readable code too.
|
|
|
|
|
The "Best Practice" is not to use string concatenation in a loop. The reason is that under the hood when concatinating two strings, a third string will be created large enough to bold both source strings. The source strings will be copied to that new string, the original string destroyed, then recreated and the contents of the temporary string copied into it, then the temporary string destroyed. The concatination that you are showing should be fine, unless it is being performed in a loop.
|
|
|
|
|
You are talking about the real Best Practice.
But the "Best Practice" is to replace any string concatenation, even in consts, by a StringBuilder.
|
|
|
|
|
const string sql = "select stuff "+
"from table";
|
|
|
|
|
KRucker wrote: The "Best Practice" is not to use string concatenation in a loop.
It still depends on what the "string" is.
And it also depends on the impact of the code under use. Most of the time a builder is pointless because it does nothing but obfuscate the code.
|
|
|
|
|
KRucker wrote: The concatination(sic) that you are showing should be fine, unless it is being performed in a loop. How many const declarations run in a loop? Unless you are instantiating a class in a loop... which indicates the const field should also be static.
You should really read posts more clearly, he wasn't complaining about the use of concatenations, but about people spouting off about best practices where the practices clearly don't apply. Makes those people look like they are doing things by rote without thinking about what they are saying or if what they say makes sense.
Of course I have been guilty of that, I shouldn't throw stones when I live in a house with glass all around it.
You forgot to mention each of those string parts get stuck in the intern table until the GC gets around to cleaning it up.
PS "that" I am guilty of includes: misreading items, not thinking things out, and saying inappropriate things.
PPS Edited my post because Preview clearly showed your statement. On checking why posting didn't show, I missed some HTML tags that removed it from post.
|
|
|
|
|
Well, best practices are not always the best...
|
|
|
|
|
"Best practices" are "at best" someone's opinion. In some cases that opinion may be shared by many, but that doesn't always make it right. After all, at one time, how many people had the opinion the world was flat and the best practice was not to sail too far out?
While some things that are considered a best practice I do see reason to use over alternatives, I really don't like the idea of having an arbitrary list of "do these things for best results". They (you know, the "they" that killed Kenny) might as well call it "'boxes to use and not think outside of' practices" instead of "best practices".
|
|
|
|
|
Chad3F wrote: After all, at one time, how many people had the opinion the world was flat and
the best practice was not to sail too far out?
At the time when people did in fact think the world was flat then in fact is was foolhardy to sail too far out because one was very likely to not return. Which was a loss both in lives and commerce.
|
|
|
|