ADO.NET - 是什么让DataReader的价值的最佳实践?价值、ADO、NET、DataReader

2023-09-03 17:33:18 作者:哀。

只是想知道究竟是什么让一个DataReader的价值的最佳实践。例如:

Just wondering what is best practice for getting the values of a datareader. For example:

我一直在做这样的:

MyValue = Dr("ColumnName")

不过,已经注意到其他人在做什么:

But have noticed other people doing:

MyValue = Dr.GetString("ColumnName")

我intested知道第二个方法的典型应用该类超支

I am intested to know the benifits of the 2nd method

推荐答案

我一直在使用的包装,做了类似的事情,做的伎俩很好了一会儿。

I've been using a wrapper that does a similar thing for a while that does the trick nicely.



     RowHelper helper = new RowHelper(Dr);
     MyValue = helper.IntColumn("ColumnName");

有关这样做的好处是助手将返回pre定义的默认NULL列(这是可配置)。我有方法,所有主要的SQL数据类型。

The nice thing about this is the helper will return pre-defined defaults for NULL columns (this is configurable). I've got methods for all the main SQL data types.

要回答你的问题,第二个方法具有一定的装箱/拆箱的开销,但它很可能是在浪费你的时间来优化这个对于一个典型的商业应用。

To answer your question, the second method has some boxing / unboxing overhead but it would probably be a waste of your time to optimise this for a typical business application.