1
good morning
How do I fill a List with a return of an sql?
What I tried to do for sure will give me an outofrange Exception
List<string> list = new List<string>();
string query = "select * from pedidos_distribuidos";
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
{
using (SqlCommand cmd = new SqlCommand(query))
{
con.Open();
cmd.CommandTimeout = 300;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
list[0] = reader[0].ToString();
}
//using (SqlDataAdapter sda = new SqlDataAdapter())
//{
// cmd.Connection = con;
// sda.SelectCommand = cmd;
// using (DataTable dt = new DataTable())
// {
// sda.Fill(dt);
// pedidos = sda.A ;
// }
//}
}
}
What am I doing
var list = "";
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
{
list = con.Query<string>("select * from pedidos_distribuidos" ).ToString() ;
}
int total = list.Count();
if (!String.IsNullOrWhiteSpace(parametrosPaginacao.SearchPhrase))
{
// areaClientes = areaClientes.Where("Area.Contains(@0) OR DescricaoGAreaCliente(@0)", parametrosPaginacao.SearchPhrase);
list = list.Where("Protocolo.Contains(@0)", parametrosPaginacao.SearchPhrase).ToString();
}
var pedidosPaginados = list.OrderBy(parametrosPaginacao.CampoOrdenado).Skip((parametrosPaginacao.Current - 1) * parametrosPaginacao.RowCount).Take(parametrosPaginacao.RowCount);
//int total = 0;
DadosFiltrados dadosFiltrados = new DadosFiltrados(parametrosPaginacao)
{
rows = pedidosPaginados.ToList(),
total = total
};
return dadosFiltrados;
}
would just do that:
list.Add(reader[0].ToString());
but, it’s much easier if you use Dapper as the AR put– Rovann Linhalis
Turns out this rapper is returning me given 12 times more than normal @Rovannlinhalis
– gabrielfalieri
What do you mean? I don’t understand
– gabrielfalieri
@Rovannlinhalis put there
– gabrielfalieri
Let’s go continue this discussion in chat.
– gabrielfalieri
Even so when I put list = con. Query<string>("select Protocol from pedidos_distributed" ).Tostring(); it returns me 48
– gabrielfalieri
For God’s sake gabrielfalieri, you need to study the concepts of language before bro. list = con.Query<string>("select * from pedidos_distributed" ).Tostring() ; You get more results because you give a select * from (which should return an object and not string) and you give me a list.Tostring...
– Gustavo Santos
@Gustavosantos be a little more polite please?
– gabrielfalieri
I didn’t disrespect you, but of course you don’t know the language and you’re doing something outside of what you know, it’s a tip, studying the language first.
– Gustavo Santos