Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Lightest list to traverse data from data reader

What I have tried:

Hello all,
I want to add data to list. can any help me to find any other way without creating the new objects of class. take a look on this.

C#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace list
{
    class Program
    {
        static void Main(string[] args)
        {

            using (SqlConnection con = new SqlConnection("server=DESKTOP-304L752\\MSSQL2016CTP2;Initial Catalog=AC_001;UId=sa;pwd=eci@123;"))
            {
                try
                {
                    using (SqlCommand cmd = new SqlCommand("Select empID,salary from Employee order by salary", con))
                    {
                        SqlDataReader dr;
                        List<Employee> emps = new List<Employee>();
                               // int[] a;
                        con.Open();
                        dr = cmd.ExecuteReader();
                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                Employee e = new Employee();
                                e.empID = dr["empID"].ToString();
                                e.salary = (int)dr["salary"];
                                emps.Add(e);
                               
                            }
                            con.Close();
                        }
                    }

                }
                catch (Exception ex)
                {

                    throw ex;
                }
                

            }
        }
    }
    class Employee
    {
        public  string empID { get; set; }
        public  int salary { get; set; }
    }
}



I just want to know the lightest element so that i can traverse the data from datareader should be kept in memory.
I don't want to create the new Objects of Employee Class because i have to iterate through more than 1 milion of records. So i don't want to create 1 milion objects for adding to list . list is capable of adding list items at run time.

I want Like this
C#
List<string> result = new List<string>();
using (conn)
{
    conn.Open();
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while(reader.Read())
        {
            result.Add(Convert.ToString(reader["Health Insurance NO"]));
        }
    }
}

but two dimensional list like list of objects of class. Plz give your comments. Your comments will be very helpful to me.
Posted
Updated 17-May-16 14:19pm
v3
Comments
OriginalGriff 17-May-16 14:42pm    
I'm not sure what happened here - but that code won't compile and it's not obvious exactly what you want to do.
Could you edit it, sort out the code, and add a fuller explanation? This is not a good question - we cannot work out from that little what you are trying to do. Remember that we can't see your screen, access your HDD, or read your mind - we onlt get exactly what you tell us, so we don;t have any context for your app otehr than teh code you show.
Use the "Improve question" widget to edit your question and provide better information.
Sergey Alexandrovich Kryukov 17-May-16 15:05pm    
Excuse me, where is your question?
—SA
Simon Bridge 18-May-16 0:38am    
It is pretty hard to work out from your question, however this did occur:

If you don't want to iterate through a million employee records, have you considered adding a where-clause to the select command? or a TOP 1000 even? because your Employee class is about as light-weight as it is possible to be, and I'm not sure moving the declaration of the 'emps' list would make any difference.

1 solution

On the little info you've shown, you need (before using SqlConnection)

C#
List<Employee> EmployeesFromDB = new List<Employee>();


then after

Quote:
Employee e = new Employee()


you would populate Employee 'e' with the columns from the data row, and

C#
EmployeesFromDB.Add(e);


but best you heed OriginalGriff & Sergey's advice first, this could be completely wrong based on the little you have shown - the quality of an answer has a direct relationship on the either the requirements shown in the question or the ability of someone out here to read your mind
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900