I used the method below to add parameters using the interface IDbCommand
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EstudoDotNet.Comum.Dominio;
using System.Data.SqlClient;
using System.Data;
public List<Laboratorio> ListarLaboratorio(int IdCidade)
{
#region Conexão
DALManager objDalManager = new DALManager();
string connectionString = objDalManager.StringConexao;
string connString = objDalManager.StringConexao;
#endregion
#region Comando
StringBuilder sbQuery = new StringBuilder();
sbQuery.Append("SELECT IdLaboratorio, NmLaboratorio, IdCidade ");
sbQuery.Append("FROM Laboratorio lab ");
#endregion
#region Filtros
StringBuilder sbFiltro = new StringBuilder();
if (IdCidade != null)
sbFiltro.Append("lab.IdCidade = @IdCidade");
if (!string.IsNullOrEmpty(sbFiltro.ToString()))
sbQuery.Append(" WHERE ").Append(sbFiltro.ToString());
#endregion
#region Oredencao
sbQuery.Append(" ORDER BY NmLaboratorio ");
#endregion
#region Execução
List<Laboratorio> lista = new List<Laboratorio>();
try
{
using (IDbConnection conn = new SqlConnection(connString))
{
conn.Open();
using (IDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sbQuery.ToString();
if (IdCidade != null)
{
cmd.Parameters.AddWithValue("@IdCidade", IdCidade);
}
cmd.Connection = conn;
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Laboratorio laboratorio = new Laboratorio();
laboratorio.IdLaboratorio = Convert.ToInt64(reader["IdLaboratorio"]);
laboratorio.NmLaboratorio = reader["NmLaboratorio"].ToString();
lista.Add(laboratorio);
}
}
return lista;
}
}
}
catch (Exception ex)
{
throw ex;
}
#endregion
}
Returns the following error:
ItriedtoreferenceSystem.Dataq.IDataParameter
,butIdidnotfindit.
Availableoptionsforcmd.Parameters.
:
Changing to SqlCommand
works normally.
How to fix this error?