Add parameters to Idbcommand

Asked

Viewed 133 times

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: inserir a descrição da imagem aqui

I tried referencing System.Dataq.IDataParameter, but I didn’t find.

Options available for cmd.Parameters.: inserir a descrição da imagem aqui

Change to SqlCommand works normally.

How to fix this error?

  • I did not understand your doubt. You can give more details or explain otherwise?

  • @bigown I added more information.

  • Put in your directions using at the beginning of the archive.

  • I’m thinking that the problem is somewhere else. I can’t see why this error is happening. The namespaces necessary are there. I don’t know if you are changing consistently. Something tells me that your code is worse now than before.

  • @Bigown Beginners are able to commit unspeakable and incomprehensible barbarism. The worst is that with SqlCommand works. The method returns a List to fill a DropDownList of laboratories according to the city selected in another DropDownList,

  • You can’t do what you’re doing. You’re editing and every hour the question is different than it was. There is no way anyone can answer and the answer is right. Ask one thing and keep the question that way. You can add information to help answer that but not completely change what you were asked.

  • To add to what @bigown said, if you want to change the question, the best is to open a new question, leave the current one intact and accept one of the answers

  • @Bigown Sorry I have no intention of disturbing or compromising the quality of the forum. And even with the issues the scope of the question remained the same. I only added more information to facilitate help, so much so that the star responded with the solution. I hope this incident does not occur again, if perhaps something similar happens I already know that the way is to create a new post. Once again I’m sorry.

  • No, the question is completely different from the original version, even if the subject is the same. This is not a forum but even in a forum if you edit the question, it makes it impossible. The difference is that a forum you can on that topic complementing new questions. Here we have no topics, we have questions, only. dcastro responded to the current version, not the original version. Cigano responded to the original version, his response is invalid for the current version.

Show 4 more comments

1 answer

1


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

When using the interface IDbCommand, parameters shall be created using the IDbCommand.CreateParameter 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);
    }

    //...    
}
  • 1

    The question changed a few times, so it became complicated to answer.

  • 1

    @bigown I have not seen the previous state of the question... but in its present state, it is understandable. OP is trying to call the method AddWithValue on the property IDbCommand.Parameters

  • 2

    Yes, but then each one will answer something different, and no answer will be valid because it always changes the question.. This question is all compromised.

  • @dcastro Thank you very much worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.