用于从数据读取器添加数据的轻量级列表 [英] Light weight list for adding data from data reader

查看:61
本文介绍了用于从数据读取器添加数据的轻量级列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最轻的列表来遍历数据阅读器中的数据



我尝试过:



Hello all,

我想将数据添加到列表中。可以帮助我找到任何其他方式,而无需创建类的新对象。看看这个。



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.

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; }
    }
}







我只想知道最轻的元素这样我可以遍历来自datareader的数据应保存在内存中。

我不想创建新的Employee Class对象,因为我必须遍历超过1百万条记录。所以我不想创建1个百万个对象来添加到列表中。 list能够在运行时添加列表项。



我想要这样




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

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"]));
        }
    }
}



但是二维列表,如类对象列表。 Plz发表您的意见。您的评论对我非常有帮助。


but two dimensional list like list of objects of class. Plz give your comments. Your comments will be very helpful to me.

推荐答案

在您显示的小信息上,您需要(在使用SqlConnection之前)



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

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





然后





then after

Quote:

员工e =新员工()





您将使用数据行中的列填充员工'e',并且





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

EmployeesFromDB.Add(e);





但是你最好注意OriginalGriff&谢尔盖的建议首先,根据你所展示的一点点,这可能是完全错误的 - 答案的质量与问题中显示的要求或者有人在这里阅读你的思想的能力有直接关系



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


这篇关于用于从数据读取器添加数据的轻量级列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆