2
I need to take the files that are in a list and compare with the files that are in a table in the database, and know if the amount of files that is in my list is the same amount as the table.
using Dapper;
public bool ValidarPessoas(List <int> pessoas) {
Dictionary < string, object > parametros = new Dictionary < string, object > ();
parametros.Add("@pessoas", pessoas);
parametros.Add("@quantidadePessoas", pessoas.Count);
var query = @ " SELECT CASE WHEN EXISTS
(
SELECT COUNT(ID) FROM dbo.[Pessoa] WHERE ID in (@pessoas) HAVING COUNT(Pessoa.ID) = @quantidadePessoas
)
THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT)
END ";
string strConexao = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString;
using(var sqlConnection = new SqlConnection(strConexao))
{
return sqlConnection.QueryFirstOrDefault < bool > (query, parametros);
}
}
When I run this code, I get the error:
System.Data.Sqlclient.Sqlexception: 'Incorrect syntax near ','.'
You can include the value of the variable in the question
query
before executing?– Ricardo Pontual
I posted an answer, the idea is good, but I do not know if the code has typo, if you have correct please :)
– Marconi
Thank you so much for the answer, Marconi. We took your code as the basis for perfecting ours. It worked perfectly
– Emmanuel
Nice Emmanuel, too good I could help. If there’s something wrong with my code please edit my answer because it might be for future readers of the post!
– Marconi