I need to deserialize information from a database in C#
, I have the following SQL
in C#
:
select
Id,
Email,
Name,
Login
FROM usuarios
WHERE Login = @login
and Senha = CONVERT(VARCHAR(2000), HASHBYTES('MD5', @password) ,2)
And to deserialize I'm doing the following:
while (reader.Read())
{
toReturn.Id = int.Parse(reader["Id"].ToString());
toReturn.Email = reader["Email"].ToString();
toReturn.Name = reader["Name"].ToString();
toReturn.UserName = reader["Login"].ToString();
}
However, it never returns any value and throws no exception. What am I doing wrong?
Querying like this:
select
Id,
Email,
Name,
Login
FROM usuarios
WHERE Login = 'usuario'
and Senha = CONVERT(VARCHAR(2000), HASHBYTES('MD5', '123') ,2)
It returns the correct user.
To run, I'm using this code:
cmd.CommandText = "select Id, Email, Name, Login FROM [vw_usuarios] WHERE
Login = '@login' and Senha = CONVERT(VARCHAR(2000), HASHBYTES('MD5', '@password') ,2)";
cmd.Parameters.AddWithValue("@login", username);
cmd.Parameters.AddWithValue("@password", password);
cmd.CommandType = System.Data.CommandType.Text;
reader = cmd.ExecuteReader();
Notes:
- I already tested it in the database and it worked.
- When I remove the parameters, it returns all values.