sqlDataReader查询字符串中的连接字符串会引入虚假的“N”字符

时间:2014-02-19 04:12:45

标签: sql .net sqldatareader

我在.net 4.0中使用SqlDataReader

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

[我使用字母b表示空格字符。]

我收到的结果如'SmithN,bJohn'

如果我在wSQL字符串中省略了N限定符,我会得到相同的结果。

如果wSQL字符串更改为

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

(逗号后面有两个空格)。

我得到带有两个空格的结果'Smith,bbJohn'(当然),但N已经消失。

这是一个已知的.Net错误吗?有没有已知的工作来避免额外的空间?

上述select语句在SQL Server Management Studio中运行良好。

1 个答案:

答案 0 :(得分:0)

尝试在N ..之前删除+符号。您可能还需要LastName之后的逗号..喜欢这个:

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

我有一个类似的查询(但更复杂)也许它会帮助你:

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

它可能看起来像:

 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