简体   繁体   English

C#是否使用逐字字符串创建奇怪的换行符?

[英]Does C# create weird newlines with verbatim strings?

I was running a personal test to try and understand EnterpriseLibrary. 我正在进行个人测试以尝试了解EnterpriseLibrary。 I was doing an inline SQL query: 我正在做内联SQL查询:

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. 考虑到我的语法错误,我将此查询复制到了SQL Server中,并且可以正常工作。 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! 更新:尝试query.Replace("\\r\\n", "")有效!

UPDATE 2: Trying the semicolon delimiter, that also works! 更新2:尝试使用分号分隔符,也可以!

The mysteries of Enterprise Library... 企业图书馆的奥秘...

Why does the semicolon delimiter work? 为什么用分号分隔符起作用?

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: 因此,对于您的字符串,您将获得:

在此处输入图片说明

So your string would look like USE Database \\r\\n space(s) SET TRANSACTION....... 因此您的字符串看起来像是USE Database \\r\\n space(s) SET TRANSACTION.......

See: String C# 另请: 字符串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 ; 不能确定为什么企业库不将其视为有效的SQL,可以使用; 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", "");

Yes, it does insert new lines. 是的,它确实插入了新行。

You can do the following before executing your query to ensure new lines are deleted: 您可以在执行查询之前执行以下操作,以确保删除新行:

query = query.Replace('\n', ' ');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM