简体   繁体   English

我应该使用哪个数据对象

[英]which Data object should i use

i have a query that return only one row (always) and i want to convert this row to class object (lets say obi) 我有一个查询,仅返回一行(总是),并且我想将此行转换为类对象(让我们说obi)

i have a feeling that using data table to this kind of query is to much but i dont realy know which other data object to use 我有一种感觉,使用数据表进行此类查询非常多,但我真的不知道该使用哪个其他数据对象

data reader? 数据阅读器?

is there a way to execute sql command to data row ? 有没有一种方法可以对数据行执行sql命令?

DataReader is the best choice here - DataAdapters and DataSets may be overkill for a single row, although, that said, if performance is not critical then keeping-it-simple isn't a bad thing. DataReader是这里的最佳选择-对于单个行,DataAdapters和DataSet可能会显得过大,尽管如此,如果性能不是很关键,那么保持简单不是一件坏事。 You don't need to go from DataReader -> DataRow -> your object, just read the values off of the DataReader and you're done. 您无需从DataReader-> DataRow->您的对象开始,只需从DataReader中读取值即可。

A datareader lets you query individual fields. 数据读取器使您可以查询各个字段。 If you want the row as a single object, I believe the DataTable/DataRowView family of objects is in fact the way to go. 如果您希望将行作为单个对象,那么我相信DataTable / DataRowView系列对象实际上是解决之道。

You might seriously consider taking a look at Linq-to-Sql or Linq-to-Entities . 您可能会认真考虑看看Linq-to-SqlLinq-to-Entities

The appeal of these frameworks is they provide automatic serialization of your database data into objects, abstract away many of the mundane details of connection management, and have better compile-time support by providing strongly-typed properties which you can use without string keys or column ordinals. 这些框架的吸引力在于,它们提供了将数据库数据自动序列化为对象的功能,抽象了许多连接管理的日常细节,并通过提供无需字符串键或列即可使用的强类型属性来提供更好的编译时支持。序。

When using Linq, the difference between retrieving a single row vs. retrieving multiple rows often only involves appending .Single() or .First() to your query. 使用Linq时,检索单行与检索多行之间的区别通常仅涉及将.Single().First()附加到查询中。

At any rate, if you already use or are willing to learn one of these frameworks, you may see the bulk and difficulty of data access code reduce substantially. 无论如何,如果您已经使用或愿意学习这些框架之一,则可能会看到数据访问代码的数量和难度大大降低。

With respect to DataReader vs. DataSet/DataTable, it is correct that it takes more cycles to allocate and populate a data table; 对于DataReader与DataSet / DataTable,正确的做法是分配和填充数据表需要更多的周期。 however, I highly doubt you will notice the difference unless creating an extremely high volume of database calls. 但是,我非常怀疑,除非创建大量的数据库调用,否则您是否会注意到差异。

In case it is helpful, here are documentation examples of data access using data readers and data sets. 如果有帮助,这里是使用数据读取器和数据集进行数据访问的文档示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM