对象集合的延迟加载
我有一个 sql 查询,可以通过 DataReader 返回大量行。 刚才我查询数据库,将结果集转换为列表,并将网格数据绑定到列表。
由于数据集的大小,这有时可能会导致超时。
我目前有一个三层设置,其中 UI 作用于业务层中的对象列表。
谁能建议在这种情况下实现延迟加载的最佳方法? 还是有其他方法可以干净地实现这一点?
我当前正在使用 Visual Studio 2005、.NET 2.0
编辑:在此实例中如何使用分页?
i have a sql query that can bring back a large number of rows via a DataReader. Just now I query the DB transform the result set into a List(of ) and data bind the Grid to the List.
This can result occasionally in a timeout due to the size of Dataset.
I currently have a three teir setup where by the UI is acting on the List of objects in the business layer.
Can anyone suggest the best approach to implementing lazy loading in this scenatrio? or is there some other way of implementing this cleanly?
I am currently using Visual Studio 2005, .NET 2.0
EDIT: How would paging be used in this instance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LINQ to SQL 似乎对您的情况有意义。
否则,如果出于某种原因,您不想使用 LINQ to SQL(例如,您使用的是 .NET 2.0),请考虑编写一个读取
DataReader
并将其转换为适当对象的迭代器:LINQ to SQL seems to make sense in your situation.
Otherwise if for any reason, you don't want to use LINQ to SQL (e.g. you are on .NET 2.0), consider writing an iterator that reads the
DataReader
and converts it to the appropriate object:需要一次性取回所有数据吗? 您可以考虑分页。
Do you need to bring back all the data at once? You could considering paging.
分页可能是您最好的解决方案。 如果您使用的是 SQL Server 2005 或更高版本,则会添加新功能。 ROWNUMBER():
有一个 示例 由 David Hayden 提供,这对于演示 SQL 非常有帮助。
此方法会减少返回的记录数量,从而减少总体加载时间。 这确实意味着您必须做更多的事情来跟踪您在记录序列中的位置,但这是值得的。
标准分页技术要求所有内容都从数据库返回,然后在中间层或客户端层(代码隐藏)进行过滤,此方法将记录减少到更易于管理的子集。
Paging might be your best solution. If you are using SQL Server 2005 or greater there was new feature added. ROWNUMBER():
There is an example by David Hayden which is very helpful in demonstrating the SQL .
This method would decrease the number of records returned, reducing the overall load time. It does mean that you will have to do a bit more to track where you are in the sequence of records, but it is worth the effort.
The standard paging technique requires everything to come back from the database and then be filtered at the middle tier, or client tier (code-behind) this method reduces the records to a more manageable subset.