Good morning
How do I populate a list with a return from 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 I'm 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;
}