SqlDataReader / DataSet到通用列表创建 [英] SqlDataReader/DataSet to Generic List Creation

查看:249
本文介绍了SqlDataReader / DataSet到通用列表创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想知道哪个是生成实体对象列表的最佳方式?

我正在使用以下获取数据的图层

演示文稿图层 - >服务图层 - > DAL(&实体层)



Way1 :



从dqladpter.Fill获取数据集()

应用linq将每个dataTable行转换为实体对象

并转换为List< entityclass>



Way2:



Hi,

I would like to know which is the best way of generating List of entity objects?
I am using following layers to fetch the data
presentation Layer->Service Layer->DAL (& Entity layer)

Way1:

get dataset from dqladpter.Fill()
apply linq to convert each dataTable row into a entity object
and convert into a List<entityclass>

Way2:

while(sqldataReader.Read())
{
EntityClass ec = new EntityClass();
ec.Name = sqldataReader["Name"];
.
.
.
ListClass.Add(ec);
}



两种方式都会返回相同的结果,但我需要哪种方法最好?为什么?

我的想法是,Way2可能是最好的,因为它减少了数据集内存大小

请澄清......

问候,
--SJ


both way return the same results but i need which is the best way and why?
My thought is, Way2 might be best because it reduce the dataset memory size
Please clarify...
Regards,
--SJ

推荐答案

你可以尝试类似于这个

首先在实体类中添加类似的方法

You can try similar to this
First of all add similar method in inside Entity Class
public EntityClass Fill(IDataReader row)
{
   return new EntityClass(row);
}


//you can write the helper method in business layer to convert to generic list
private GenericList<t> ConvertTo<t>(IDataReader reader)
                        where T : EntityClass, new()
        {
            GenericList<t> temp = new GenericList<t>();
            try
            {
                while (reader.Read())
                {
                    temp.Add((new T().Fill(reader)) as T);
                }
                return temp;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                reader.Close();
            }
        }
</t></t></t></t>





然后您可以从UI调用业务层方法调用数据访问层。 DAL将返回数据读取器实例。您可以将此实例传递给ConverTo方法以获取通用列表。希望这会给出一个提示

谢谢

Nilesh



then you can invoke the business layer method from UI inturn invoke data access layer. DAL will return the data reader instance. This instance you can pass to ConverTo method to get the generic list. Hope this will give an hint
Thanks
Nilesh


这篇关于SqlDataReader / DataSet到通用列表创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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