使用DataTables代替DataReaders:一个好习惯吗? (C#) [英] Using DataTables instead DataReaders: a good pratice? (C#)

查看:82
本文介绍了使用DataTables代替DataReaders:一个好习惯吗? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,全部!
您如何在我的DAO类上只使用DataTables而不是DataReader呢?

我想改变经典:

Hey, all!
What do you think about working only with DataTables instead DataReaders on my DAO classes?

I thought to change the classic:

public IList<client> ReadAll()  
{  
    String selectString = "SELECT * FROM CLIENTS";  
    IList<client> clients = new List<client>();  
    using (var selectCommand = new OracleCommand(connection, selectString))  
    {  
        using (OracleDataReader selectReader = selectCommand.ExecuteReader())  
        {   
           if (selectReader.HasRows)  
           {  
               while (selectReader.Read())  
               {  
                   clientes.add( GetDomainObject(selectReader) );  
               }  
               selectReader.close();  
           }  
        }
    }  
    return clients;
}  
</client></client></client>



通过这种方式将数据从数据库获取到我的集合:



for this way to get data from database to my collection:

public IList<client> ReadAll()  
{  
    String selectString = "SELECT * FROM CLIENTS";  
    IList<client> clients = new List<client>();  
    using (var adapter = new OracleDataApdater(connection, selectString))  
    {  
        DataTable datatableClients = new DataTable("CLIENTS");  
        adapter.Fill(datatableClients);  
        foreach (DataRow datarowClient in datatableClients.Rows)  
        {  
            clients.add( GetDomainObject(datarowClient) );  
        }  
    }  
    return clients;
}  
</client></client></client>



在我的DAO类上进行此更改时,您看到任何好处吗?
谢谢大家!



Do you see any advantage in do this change on my DAO classes?
Thanks all!

推荐答案

两者都给出了期望的结果.除了DataReader相对较快之外,但由于它可与独占数据库连接一起使用,因此具有成本.如果要使用断开连接的方案,则最好使用DatatableDataset.有关更多信息,请参见MSDN论坛 DataTable与DataReader [ ^ ]
Both give the desired out put. Except DataReader relatively faster but has cost due to it works with exclusive database connection. If you want work with disconnected scenario, then Datatable or Dataset is much preferable. For more look this MSDN forum DataTable Vs DataReader[^]


真是不可思议. +5.

Datareader在连接的体系结构中工作,而Dataset,Datatable在断开的体系结构中工作.
Connected Architecture保持连接,直到关闭读者.并从数据库中获取更新的记录.
但是,将阅读器用于大量记录是不好的编码习惯,这可能会降低性能.

如果您使用的是Datareader,则需要在使用后通过使用 Close()Dispose()释放内存并将其设置为 NULL
此处可能发生的其他明智的OUT_OF_MEMORY_EXCEPTION是参考 [
So true Wonde. +5.

Datareader works in connected Architecture whereas Dataset, Datatable with disconnected Architecture.
Connected Architecture hold a connection until reader closed. and fetched updated records from database.
But use of readers for large nunmbers of records is not good coding practics which may slow down the performance.

If you are using Datareader then you need to free up memory after use by using Close() Dispose() and make it NULL
Other wise OUT_OF_MEMORY_EXCEPTION could occours here is ref[^].


阅读器允许您过滤(或处理)要添加到集合中的行,而无需将整个批次加载到内存中.如果您正在编写的功能是将全部内容存储到内存中(如此处所示),那么这两种方法之间几乎没有什么区别,并且正如Wonde所提到的,DataTable方法具有边际优势.在大多数应用程序中,您只需将所有结果行加载到内存中,然后对其进行处理即可.只有在您的查询返回的行(百万)的数目非常可笑时,您才需要线性处理它们而不预先加载所有行.
The Reader allows you to filter (or otherwise process) the rows you want to add to your collection, without loading the whole lot into memory. If what you''re writing is a function to get the whole lot into memory (as here), then there is little difference between these two approaches, and as Wonde mentions there are marginal benefits to the DataTable approach. In most applications you do just load all the result rows into memory and then process them – it''s only if your query will return a ridiculous number of rows (millions) that you would want to process them linearly without pre-loading them all.


这篇关于使用DataTables代替DataReaders:一个好习惯吗? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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