SqlDataAdapter的VS SqlDataReader的SqlDataAdapter、VS、SqlDataReader

2023-09-02 11:45:14 作者:打板哥

什么是使用的SqlDataAdapter VS SqlDataReader对象从一个数据库获取数据之间的差异?

What are the differences between using SqlDataAdapter vs SqlDataReader for getting data from a DB?

我特别是寻找到自己的优点和缺点,以及他们的速度和内存性能。

I am specifically looking into their Pros and Cons as well as their speed and memory performances.

感谢

推荐答案

保持连接打开,直到完成(不要忘记关闭它!)。 通常只能用一次迭代 是不是更新到数据库有用

在另一方面,它

唯有在一个时间内存中的一个记录,而不是整个结果集(这可能是巨大的) 讲述的是尽可能快,你可以​​得到一个迭代 允许您启动的处理结果早 让你尽快它完成加载数据关闭连接,甚至可能会自动为你关闭它 所有的结果都在内存 您可以迭代它多次,因为你需要,甚至可以查找一个特定的纪录指数 有一些内置的院系更新到数据库

目前的成本:

的许多的更高的内存使用 您等到所有的数据都使用它的任何加载之前 Much higher memory use You wait until all the data is loaded before using any of it

因此​​,其实这取决于你在做什么,但我倾向于preFER一个DataReader,直到我需要的东西,唯一支持的数据集。 SqlDataReader的是完美的结合只读格公用数据访问的情况下

So really it depends on what you're doing, but I tend to prefer a DataReader until I need something that's only supported by a dataset. SqlDataReader is perfect for the common data access case of binding to a read-only grid.