Just wondering what is best practice for getting the values of a datareader. For example:
只是想知道获取datareader值的最佳做法是什么。例如:
I have been doing this:
我一直这样做:
MyValue = Dr("ColumnName")
But have noticed other people doing:
但是注意到其他人在做:
MyValue = Dr.GetString("ColumnName")
I am intested to know the benifits of the 2nd method
我很想知道第二种方法的好处
5 个解决方案
#1
3
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");
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.
关于这一点的好处是帮助器将返回NULL列的预定义默认值(这是可配置的)。我有所有主要SQL数据类型的方法。
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.
要回答你的问题,第二种方法有一些装箱/拆箱开销,但这可能是浪费你的时间来优化典型的业务应用程序。
#2
4
The DbDataReader.GetString(int) method can only be used if you know the index of the column. Using DbDataReader.GetString("ColumnName") is not possible as there is no such overload. I.e. you have the following two options:
只有知道列的索引时才能使用DbDataReader.GetString(int)方法。使用DbDataReader.GetString(“ColumnName”)是不可能的,因为没有这样的重载。即您有以下两种选择:
string myValue1 = (string) Dr["ColumnName"];
string myValue2 = Dr.GetString(columIndex);
The first line internally calls DbDataReader.GetOrdinal("ColumnName").
第一行内部调用DbDataReader.GetOrdinal(“ColumnName”)。
#3
3
I made a couple of generic extension methods for that:
我为此做了几个通用的扩展方法:
public static class DataReaderExtensions
{
public static T GetValueOrNull<T>(this IDataRecord reader, string fieldName)
where T : class
{
int index = reader.GetOrdinal(fieldName);
return reader.IsDBNull(index) ? null : (T)reader.GetValue(index);
}
}
You get the picture... I also have a GetValueOrDefault
for value types.
你得到了图片......我也有值类型的GetValueOrDefault。
#4
1
DataReader returns type object so you need to cast the value from the DataReader to whatever type MyValue is so in the case of int
DataReader返回类型对象,因此您需要将DataReader中的值转换为MyValue的任何类型,以便在int的情况下
MyValue = (int)Dr("ColumnName");
or
要么
MyValue = Convert.ToInt(Dr("ColumnName"));
or
要么
MyValue = Dr("ColumnName").ToInt();
I'm not sure of performance differences but I think that could be construed as micro-optimizing unless working on extremely large data sets
我不确定性能差异,但我认为这可以解释为微优化,除非处理非常大的数据集
#5
0
Using AutoMapper:
使用AutoMapper:
var dataReader = ... // Execute a data reader
var views = Mapper.Map<IDataReader, IEnumerable<SomeView>>(_dataReader);
#1
3
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");
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.
关于这一点的好处是帮助器将返回NULL列的预定义默认值(这是可配置的)。我有所有主要SQL数据类型的方法。
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.
要回答你的问题,第二种方法有一些装箱/拆箱开销,但这可能是浪费你的时间来优化典型的业务应用程序。
#2
4
The DbDataReader.GetString(int) method can only be used if you know the index of the column. Using DbDataReader.GetString("ColumnName") is not possible as there is no such overload. I.e. you have the following two options:
只有知道列的索引时才能使用DbDataReader.GetString(int)方法。使用DbDataReader.GetString(“ColumnName”)是不可能的,因为没有这样的重载。即您有以下两种选择:
string myValue1 = (string) Dr["ColumnName"];
string myValue2 = Dr.GetString(columIndex);
The first line internally calls DbDataReader.GetOrdinal("ColumnName").
第一行内部调用DbDataReader.GetOrdinal(“ColumnName”)。
#3
3
I made a couple of generic extension methods for that:
我为此做了几个通用的扩展方法:
public static class DataReaderExtensions
{
public static T GetValueOrNull<T>(this IDataRecord reader, string fieldName)
where T : class
{
int index = reader.GetOrdinal(fieldName);
return reader.IsDBNull(index) ? null : (T)reader.GetValue(index);
}
}
You get the picture... I also have a GetValueOrDefault
for value types.
你得到了图片......我也有值类型的GetValueOrDefault。
#4
1
DataReader returns type object so you need to cast the value from the DataReader to whatever type MyValue is so in the case of int
DataReader返回类型对象,因此您需要将DataReader中的值转换为MyValue的任何类型,以便在int的情况下
MyValue = (int)Dr("ColumnName");
or
要么
MyValue = Convert.ToInt(Dr("ColumnName"));
or
要么
MyValue = Dr("ColumnName").ToInt();
I'm not sure of performance differences but I think that could be construed as micro-optimizing unless working on extremely large data sets
我不确定性能差异,但我认为这可以解释为微优化,除非处理非常大的数据集
#5
0
Using AutoMapper:
使用AutoMapper:
var dataReader = ... // Execute a data reader
var views = Mapper.Map<IDataReader, IEnumerable<SomeView>>(_dataReader);