参数未添加到sql字符串中,我在做什么错呢?

SqlCommand comando = conexao.CreateCommand();
comando.CommandTimeout = 7200;
foreach (SqlParameter parametro in parametros)
{
    comando.Parameters.Add(new SqlParameter(parametro.ParameterName, parametro.Value));
}

comando.CommandText = cmdSql;
comando.CommandType = CommandType.Text;
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = comando;

try
{
    adapter.Fill(dt);
}
catch (Exception ex) { Console.Write(ex.Message); }
conexao.Close();

return dt;




带有上述方法的方法的Sql String。

string cmdSql = "select top @quantidade *  from representante";
SqlParameterCollection sqlParameters = new SqlCommand().Parameters;
sqlParameters.AddWithValue("@quantidade", SqlDbType.Int).Value = quantidade;

return Persistencia.Persistencia.ConsultaComando(cmdSql, sqlParameters);

最佳答案

感谢所有帮助,但解决方案是在MSDN中回答了string cmdSql = "select top (@quantidade) * from representante";的使用:
Answer in MSDN BRASIL

09-25 19:45