1

This is working in VB.Net

Dim sSQL As String
sSQL = <sql>MySQLQueryHere</sql>.Value

But, this is not working in C#

string sSQL = "";
sSQL = <sql>MySQLQueryHere</sql>.Value;

The below part is showing error after build.

    sSQL = <sql>MySQLQueryHere</sql>.Value;

Any suggestion about what I am doing wrong?

Sixthsense
  • 1,927
  • 2
  • 16
  • 38
  • This doesn't make sense, C# doesent have <>> tags. If you want c# code look in to this https://stackoverflow.com/a/21709663/5830472 – PrathapG Dec 12 '18 at 05:31
  • 1
    I know your question has been answered, but please can you include the error you're receiving in case it helps future readers. – ProgrammingLlama Dec 12 '18 at 05:37

3 Answers3

6

That is an XML literal and they are not supported in C#. They are often used in VB when writing SQL queries to allow the text to be split over multiple lines without having to cloud things with lots of quotes and ampersands. They are no longer required in VB because VB 2015 and later support multiline string literals. C# also supports multiline string literals and I think has for longer, although I'm not sure from what version.

This is example of VB code using an XML literal:

Dim sql = <sql>
            SELECT *
            FROM MyTable
          </sql>
Dim query = sql.Value 'This is a String

Here's the equivalent VB using a multiline string literal:

Dim query = "SELECT *
             FROM MyTable"

Here's the equivalent C# code:

var query = @"SELECT *
              FROM MyTable";

In the C# code, note that you must use a verbatim string literal in order to spread it over multiple lines. A verbatim string literal is one prefixed with an @ to indicate that every character between the quotes should be taken literally. These are often used for file and folder paths, so that you don't need to escape all the slashes. It does mean that you can't escape anything in the text though.

Also be aware that, in all three cases, the whitespace at the head of each line is included as part of the text as well. This is no big deal for SQL code but there are times where that would be a problem, in which case you have to push all the text to the left in the editor. This doesn't look great but it's a necessary trade-off.

I should also point out that the XML literal code is not a strict equivalent of the others. Strictly speaking, the first code snippet would have to be like this to be equivalent:

Dim sql = <sql>SELECT *
               FROM MyTable</sql>
Dim query = sql.Value 'This is a String

In my opinion though, XML literals are easier to read when written as XML normally would be but there's no reason to include the extra whitespace and line breaks in a multiline string literal. Again, it's of no consequence for SQL code but should be considered when whitespace matters.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
1

XML Literals are only available in VB, not in c#. I suspect what you want to do is make a multiline string literal? To do so in c#, you can use the @ string prefix to make a verbatim string, like so:

string sSQL = @"SELECT col1, col2
FROM table
ORDERBY col2";
SirCxyrtyx
  • 387
  • 3
  • 10
0

Referencing the previous two answers:

var query = @"SELECT *
              FROM MyTable";

there is also another way in writing query in C# as below:

string sSQL = "SELECT col1, col2 "+
              "FROM table "+
              "ORDERBY col2";

but stay careful in adding spaces to the end of each string in each line to maintain space for each of them.

Rashedul.Rubel
  • 3,446
  • 25
  • 36