0

I have a constant string as shown below

const String GetSQL = @"select
            id
        from
            emp

        ";

When I try to use the same in a method the value for the GetSQL is with the linebreaks i.e. select \r\n id\r\n etc.

How can I get it in one line without those breaks

Thanks

acadia
  • 2,301
  • 10
  • 40
  • 57
  • possible duplicate of [Replace Line Breaks in a String C#](http://stackoverflow.com/questions/238002/replace-line-breaks-in-a-string-c) – kemiller2002 Aug 16 '10 at 18:11
  • Why do you want to declare the statement in that format? Why don't you want line breaks in the end result? Let us know what you're trying to do here. – bdukes Aug 16 '10 at 18:42

7 Answers7

5

Is there some reason not to use: SELECT id FROM emp all on one line?

AllenG
  • 8,112
  • 29
  • 40
  • To learn the answers, one must know the correct questions to ask. Or in this case, leave it up to SO to give you the answer to your REAL problem. +1. – xdumaine Aug 16 '10 at 18:16
1

You have a few choices:

  1. Get rid of the line breaks, and format the SQL onto a single line. This works well for a simple SQL query, but can get cumbersome if the SQL is large and complex.
  2. Programmatically remove the breaks using string.Replace("\n"," "). This can work well, but can be fragile - especially if you rewrite a single-line statement into a multi-line statement later.
  3. Don't put the string directly in your code, but read it from a resource or file. This can allow you to better organize your SQL queries, but you may still need to remove undesired breaks (using replace as above).
  4. Use a stored-procedure to avoid embedding the SQL directly in your code to begin with. It's a best practice, but it can require more significant changes to your code, and may not always be possible (for example, department policies may prohibit it).
LBushkin
  • 129,300
  • 32
  • 216
  • 265
1

Just declare it without line breaks. You can still put it on different lines using concatenation.

const string sql = "SELECT"
                 + "     id"
                 + " FROM"
                 + "     emp";
bdukes
  • 152,002
  • 23
  • 148
  • 175
0

programmatically remove the breaks.

akonsu
  • 28,824
  • 33
  • 119
  • 194
0

Can't you declare it as:

const String GetSQL = "select id from emp";

?

Ignacio
  • 7,947
  • 15
  • 63
  • 74
0

Assuming you can't modify the constant value itself (as in put in all on one line in the code), this will show you how to replace the line breaks:

Replace Line Breaks in a String C#

line = line.Replace(Environment.Newline, String.Empty)
Community
  • 1
  • 1
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
0
"Your String".Replace("\r", "").Replace("\n", "")

or just

"Your String".Replace(Environment.Newline, "replacement text")
Jon Smock
  • 9,451
  • 10
  • 46
  • 49