简体   繁体   中英

Concatenated strings in sqlDataReader query string introduce spurious 'N' character

I am using a SqlDataReader in .net 4.0

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.]

I am getting results like 'SmithN,bJohn'

I get the same result if I omit the N qualifier from within the wSQL string.

If the wSQL string is changed to

"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.

Is this a known .Net bug? Is there a known work around to avoid the extra space?

Both the above select statements run fine in SQL Server Management Studio.

Try removing the + sign before N..you may also need a comma after LastName.. Like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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