铸造Ado.net的DataReader IDataRecord给奇怪的结果 [英] Casting Ado.net DataReader to IDataRecord giving strange result

查看:103
本文介绍了铸造Ado.net的DataReader IDataRecord给奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我对数据库运行一个查询,我可以看到有对31/05/2013纪录。当我运行从C#与ADO.NET此查询,然后使用下面的code,我缺少了31/05/2013记录

I have a query that I run against the database, and I can see that there is a record for 31/05/2013. When I run this query from C# with ADO.NET, and then use the following code, I am missing the record for 31/05/2013

var timeSeriesList = new List<TimeSeries>();  
using (var reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        timeSeriesList = reader.Cast<IDataRecord>()
            .Select(r => new TimeSeries
                 {
                     MidRate = (double)r["MidRate"],
                     RiskFactorName = (string)r["RiskFactorName"],
                     SeriesDate = (DateTime)r["SeriesDate"]
                 }).ToList();

    }
}

但是,如果我用的是相同的查询与此code:

However, if I use the same query with this code:

var timeSeriesList = new List<TimeSeries>();                        
using (var reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        var timeSeries = new TimeSeries
                 {
                     MidRate = (double)reader["MidRate"],
                     RiskFactorName = (string)reader["RiskFactorName"],
                     SeriesDate = (DateTime)reader["SeriesDate"]
                 };

        timeSeriesList.Add(timeSeries);
    }
}

...然后在31/05/2013记录在集合中 - 为什么会的code中的第一个块给这个结果呢?

...then the record at 31/05/2013 is in the collection - why would the first block of code give this result?

推荐答案

我认为你缺少的第一个例子中的记录,因为你一个移动阅读器,然后将它转换。

I think that you are missing record in first example because you move reader by one and then cast it.

试试这个变化,看看它的工作:

Try this change and see if it worked:

var timeSeries = new List<TimeSeries>();  
using (var reader = cmd.ExecuteReader())
{
    if (reader.HasRows)
    {
        timeSeries = reader.Cast<IDataRecord>()
            .Select(r => new TimeSeries
                 {
                     MidRate = (double)r["MidRate"],
                     RiskFactorName = (string)r["RiskFactorName"],
                     SeriesDate = (DateTime)r["SeriesDate"]
                 }).ToList();
    }
}

这篇关于铸造Ado.net的DataReader IDataRecord给奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆