如何在 VB.Net 或 ASP.Net 中创建 Windows 应用程序
使用哪种方法更有效: 从某个数据库表导入数据时使用DataAdapter和DataSet或者DataReader???
which method is more efficient to use:
DataAdapter and DataSet Or DataReader when importing data from a certain database table???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我只会选择您最喜欢的一个,并在您发现使用其中一个的瓶颈时决定进行优化。 如果您只是在规划一个新应用程序,那么让它以良好的性能运行比陷入困境并试图选择“最佳”方法更有意义。
I would just pick whichever one you like best, and decide to optimize once you find a bottleneck in using one or the other. If you're at the place of just planning a new app, it makes more sense to get it working with Okay performance, than to get bogged down trying to pick the 'best' approach.
在底层,DataAdapter 使用 DataReader 来实际查询数据并填充数据表/数据集。 所以归根结底,使用哪一个并不重要,但我建议使用 DataAdapter,因为它更容易编码,并且不易出错。
Under the hood, the DataAdapter uses the DataReader to actually query the data and populate the datatable/dataset. So end of the day, it doesn't really matter which one you use, but I would suggest the DataAdapter as it's easier to code against, and less error prone.
数据读取器将以渐进的方式读取数据,允许您一次处理一行,其速度快且重量轻,但主要只是向前。
数据适配器和数据集实际上使用 DataReader 来获取数据并将结果存储在内存中,填充表需要更长的时间,但此后的访问和更新要快得多,直到将数据保留回数据库。
如果您注重速度,请使用数据读取器,否则请使用数据集,因为它更具可读性和可管理性。
A Data reader will read the data in a progressive manner allowing you to deal with one row at a time, its fast and light weight but is mainly forward only.
A Data adapter and Dataset, acctually use the DataReader in order to get the data out and stores the result in memory filling the table takes longer but accessing and updating after this is much quicker, until you persist the data back to the DB.
Use a datareader if speed is your thing, otherwise use a dataset since its far more readable and managable.
用例在这里很重要。 一旦数据被整理到您的应用程序中,您将如何使用这些数据?
如果您只需要迭代它,则 DataReader 的内存开销是所列出的技术中最少的。
DataAdapter提供更多的能力来换取更多的内存,具体来说会涉及到DataTable的开销。 这是为了换取更方便的API。
DataSet 是最重的(至少在您获取类型化数据集之前),但提供了获取 DataRow 之间内存中关系的能力。
在内存和编程效率方面存在一系列功能与成本 - 在许多用例中内存相对便宜,从长远来看,程序员的时间和代码的可维护性可能更重要。
Use case is important here. How are you going to use the data once it's marshaled into your application?
If you need only iterate through it, a DataReader has the least memory overhead of your listed technologies.
DataAdapter provides more capabilities in exchange for more memory, and is specificaly going to involve the overhead of DataTable. This is in exchange for a more convenient API.
DataSet is heaviest (before you get to typed datasets, at least), but provides the abilitiy to get in-memory relationships between DataRows.
There is a range of capabilities versus cost in terms of memory and programming efficiency - memory is relatively cheap in many use cases, and programmer time and maintainability of code may be more important in the long run.
DataReader 比 DataAdapter 更“高效”,但它也有局限性。 这可能是最适合您的问题的,但如果没有更多细节,很难说。
正如约翰所说,找到可行的解决方案可能是您的首要任务。
A DataReader is more 'efficient' than a DataAdapter but it has limitations. It's probably the best for your problem, but without further details it is hard to say.
And as John says, get a working solution might be your first priority.
因为听起来您正在寻找程序效率。 我倾向于使用数据读取器作为访问方法的业务对象类。 这将使您能够创建可维护的代码并提高速度。
您可以使用迭代 DataReader 的方法创建一个对象,如下例所示。 这给了你所有的速度,并且在我看来它是相当可维护的。 这可能几乎就是 DataSet 类型为您提供的功能,只是不需要更多行代码来概括检索数据所需的功能。
像这样的事情:
有很多方法可以做到这一点,希望这会有所帮助
Since it sounds like you're looking for program efficiency. I would lean toward a business object class using a data reader as the access method. That would give you the ability to create maintainable code and give you speed.
You could create an object with a method that iterates through a DataReader like the example below. This gives you all the speed and in my opinion it's quite maintainable. This is probably pretty much what a type DataSet provides for you, just without more lines of code to generalize the functionality needed in retrieving data.
something like this:
There's lots of ways to do it, hope this helps