Return a list of an object

2

I have the following model with a method to retrieve a list of data of the same model.

public class Totem
{
    public int Id { get; set; }
    public string Dato1{ get; set; }
    public string Dato2 { get; set; }
    public string Dato3 { get; set; }

    public List<Totem> GetTotem()
    {
        try
        {
            using (SqlConnection conn = new SqlConnection(Connection.ConnString))
            {
                string SQL = string.Empty;

                SQL += "SELECT id, dato1, dato2, dato3";
                SQL += " FROM";
                SQL += " mitabla";

                try
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand(SQL, conn);
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);
                    DataSet dataset = new DataSet();
                    sqlDataAdapter.Fill(dataset, "mitabla");
                    conn.Close();

                    this.Id = Convert.ToInt32(dataset.Tables[0].Rows[0]["suc"].ToString());
                    this.Dato1= dataset.Tables[0].Rows[0]["dato1"].ToString();
                    this.Dato2 = dataset.Tables[0].Rows[0]["dato2"].ToString();
                    this.Dato3 = dataset.Tables[0].Rows[0]["dato3"].ToString();
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
}

Calling the GetTotem method from my controller should bring me a list of all the data I'm requesting (all data from the table of only those 4 fields) but I do not know how to return that list ...

Who can help me?

    
asked by vcasas 28.08.2018 в 21:25
source

1 answer

2

You do not have any return in that function you must initialize a list of the same type as the function add elements and return it when leaving the function, I would prefer to define that class < strong> Totem and its properties on the one hand and the GetTotem () list on another side I say this because each element you add to the list will also have the GetTotem () , something else instead of using this you can create an object of type Totem and if you are going to obtain multiple records create a cycle where you create the object you assign the values and keep them in the list:

public List<Totem> GetTotem()
{
    List<Totem> lista = new List<Totem>();
    try
    {
        using (SqlConnection conn = new SqlConnection(Connection.ConnString))
        {
            string SQL = string.Empty;
            SQL += "SELECT id, dato1, dato2, dato3";
            SQL += " FROM";
            SQL += " mitabla";
            conn.Open();
            SqlCommand command = new SqlCommand(SQL, conn);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);
            DataSet dataset = new DataSet();
            sqlDataAdapter.Fill(dataset, "mitabla");
            conn.Close();
            this.Id = Convert.ToInt32(dataset.Tables[0].Rows[0]["suc"].ToString());
            this.Dato1= dataset.Tables[0].Rows[0]["dato1"].ToString();
            this.Dato2 = dataset.Tables[0].Rows[0]["dato2"].ToString();
            this.Dato3 = dataset.Tables[0].Rows[0]["dato3"].ToString();
            lista.Add(this);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return lista;
}

My Recommendation:

    public List<Totem> GetTotem()
    {
        List<Totem> lista = new List<Totem>();
        try
        {
            using (SqlConnection conn = new SqlConnection(Connection.ConnString))
            {
                string SQL = string.Empty;
                SQL += "SELECT id, dato1, dato2, dato3";
                SQL += " FROM";
                SQL += " mitabla";
                conn.Open();
                SqlCommand command = new SqlCommand(SQL, conn);
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);
                DataSet dataset = new DataSet();
                sqlDataAdapter.Fill(dataset, "mitabla");
                conn.Close();
                foreach (DataRow row in dataset.Tables[0].Rows)
                {
                    Totem objeto = new Totem();
                    objeto.Id = Convert.ToInt32(row["suc"].ToString());
                    objeto.Dato1 = row["dato1"].ToString();
                    objeto.Dato2 = row["dato2"].ToString();
                    objeto.Dato3 = row["dato3"].ToString();
                    lista.Add(objeto);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return lista;
    }
    
answered by 28.08.2018 / 23:14
source