I am using C# and .Net 4.0 with MS SQL 2008.
I am running an integration test to verify that data is getting correctly stored and retrieved. It fails more often than not. When I look into it I see that I am getting the wrong value back from the linq-to-sql call. I have profiled the linq-to-sql statement and discovered that in Server Management Studio, the profiled SQL returns the wrong value, while a hand typed query with the same parameters works correctly.
The linq-to-sql query and result:
exec sp_executesql N'SELECT TOP (1) [t0].[ID], [t0].[UserName], [t0].TCID
FROM [dbo].[Users] AS [t0]
WHERE ([t0].[TCID] = @p0) AND ([t0].[UserName] = @p1)',N'@p0 int,@p1
nvarchar(4000)',@p0=8,@p1=N'ҭРӱґѻ'
Results in
ID UserName TCID
2535 ҭРґѻӱ 8
As you can see, UserName does not match what was in the equality check.
If I do this, I get the expected result:
SELECT TOP 1000 [ID]
,[UserName]
,[TCID]
FROM [dbo].[Users]
where TCID=8 and username = 'ҭРӱґѻ'
I get back:
ID UserName TCID
Which is correct.
UserName is nvarchar(50), ID and TCID are int.
Any ideas why the first query gets the wrong result?