Question

I was running a personal test to try and understand EnterpriseLibrary. I was doing an inline SQL query:

string query = @"USE Database
                 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
                 SELECT * FROM table
                 WHERE table.column = 'sample'"

Database, table, and column's names have been changed from their originals

However when executing this query, I received

System.Data.SqlClient.SqlException: Incorrect syntax near 'USE'.

Thinking that my syntax was wrong, I copied this query into SQL Server and it worked. I then thought that the verbatim string syntax was causing problems...so I used

string query = "USE Database " +
               "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED " +
               "SELECT * FROM table " +
               "WHERE table.column = 'sample'"

Lo and behold it worked. I am thinking the verbatim string is inputting a new line that Enterprise Library cannot parse correctly. Am I correct in assuming this? If so, how can I manipulate the verbatim string for this to work?

UPDATE: Trying query.Replace("\r\n", "") It works!

UPDATE 2: Trying the semicolon delimiter, that also works!

The mysteries of Enterprise Library...

Why does the semicolon delimiter work?

Was it helpful?

Solution

Verbatim string will add the new line character and all the extra spaces as specified in your query. So for your string you will get:

enter image description here

So your string would look like USE Database \r\n space(s) SET TRANSACTION.......

See: String C#

Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings.

Not really sure why Enterprise library doesn't consider it as a valid SQL, you can use ; to separate statements and then try like:

string query = @"USE Database;
                 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
                 SELECT * FROM table
                 WHERE table.column = 'sample';";

Or if you want to replace new line characters from your string then you can do:

query = query.Replace("\r\n", "");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top