2
Precise deserialize the information of 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 does not play any Exception. What I am doing wrong?
Query in this way:
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();
Remarks:
- I already tested it in the database and it worked.
- When I remove the parameters, it returns all the values.
Which parameters do you remove? Apparently the problem is how do you send the query, but you didn’t post that part.
– Maniero
I edited the question
– Márcio Eric
The values of
username
andpassword
at the time of this execution is as? Note that in DB is doing in the table, and in C# is doing in view, this can make a difference– Maniero
are filled with the values that are in the bank. 'username' and '123'. When I stop using the parameters and concateno with the query works, but I would like to use with the parameters.
– Márcio Eric
In your query it is different, so the problem should be this, it seems not a programming error but a data confusion.
– Maniero