Add parameters to IDbCommand

1

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?

    
asked by anonymous 26.11.2014 / 23:55

1 answer

1

The IDbCommand.Parameters queue property is of type IDataParameterCollection . This type has no method called AddWithValue , only Add (defined by the IList interface).

When using the IDbCommand interface, the parameters must be created using the IDbCommand.CreateParameter method and then added to the collection.

using (IDbCommand cmd = conn.CreateCommand())
{
    cmd.CommandText = sbQuery.ToString();
    if (IdCidade != null)
    {
        var param = cmd.CreateParameter();
        param.ParameterName = "@IdCidade";
        param.DbType = DbType.Int32;
        param.Value = IdCidade;

        cmd.Parameters.Add(param);
    }

    //...    
}
    
27.11.2014 / 11:05