简体   繁体   English

sqlDataReader查询字符串中的串联字符串引入了虚假的“ N”字符

[英]Concatenated strings in sqlDataReader query string introduce spurious 'N' character

I am using a SqlDataReader in .net 4.0 我在.net 4.0中使用SqlDataReader

Dim wSQL = "Select (LastName + N',b' + FistName) as CustomerName from Customers"
Dim wSQLDataReader as SQLDataReader = wSQLConnection.ExecuteReader(wSQL)

[I am using the letter b to denote a space character.] [我使用字母b表示空格。]

I am getting results like 'SmithN,bJohn' 我收到类似'SmithN,bJohn'

I get the same result if I omit the N qualifier from within the wSQL string. 如果我从wSQL字符串中省略了N限定符,则会得到相同的结果。

If the wSQL string is changed to 如果wSQL字符串更改为

"Select (LastName + N',bb' + FistName) as CustomerName from Customers"

(with two spaces following the comma). (逗号后面有两个空格)。

I get result 'Smith,bbJohn' with the two spaces (of course) but the N has disappeared. 我得到带有两个空格的结果'Smith,bbJohn' (当然),但是N消失了。

Is this a known .Net bug? 这是一个已知的.Net错误吗? Is there a known work around to avoid the extra space? 是否有已知的解决方法来避免多余的空间?

Both the above select statements run fine in SQL Server Management Studio. 上面的两个select语句在SQL Server Management Studio中都可以正常运行。

Try removing the + sign before N..you may also need a comma after LastName.. Like this: 尝试删除N之前的+号。LastName之后可能还需要逗号。

Dim wSQL = "Select (LastName N',b' + FistName) as CustomerName from Customers"
Dim wSQLDataReader as SQLDataReader = wSQLConnection.ExecuteReader(wSQL)

I have a query that is similar (but more complex) maybe it will help you it is: 我有一个类似(但更复杂)的查询,它可能会帮助您:

SELECT  
'All Users' as QuestionOption,
Stuff( (SELECT N'; ' + email FROM users where email>='  ' FOR XML PATH(''),TYPE) 
.value('text()[1]','nvarchar(max)'),1,2,N'') as QuestionOptionValue

It may look something like: 它可能看起来像:

 Dim wSQL = "Select 'CustomerName' as CustomerName, Stuff( (SELECT LastName N', ' +     FirstName FROM CustomerName FOR XML PATH(''),TYPE).value('text()[1]','nvarchar(max)'),1,2,N'') as Customer_Full_Name

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

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