I need to execute this Sql command (which works fine in management studio):
select * from Users where Login = 'test' and PasswordHash = HashBytes('SHA1', 'test')
I wrote this c# linqtosql:
var user = db.ExecuteQuery<User>("select * from Users where Login = {0} and PasswordHash = HashBytes('SHA1', {1})", loginTextBox.Text.Trim(), passwordPasswordBox.Password).SingleOrDefault();
but it never works!!
can anybody help me?
thanks!
This is the DataContext’s log:
select * from Users where Login = @p0 and PasswordHash = HashBytes('SHA1', @p1)
-- @p0: Input NVarChar (Size = 4000; Prec = 0; Scale = 0) [test]
-- @p1: Input NVarChar (Size = 4000; Prec = 0; Scale = 0) [test]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
I profiled it via
db.Log = Console.Out, with the result:Looking at this, I imagine the problem is that the param is
NVarCharrather thanvarchar– so taking the hash (a binary operation) is different. If you hashed it asvarchar, you should probably cast the string tovarcharbefore theHASHBYTES.The following works, for example:
A simpler illustration of this is: