【问题标题】:How to load column data from DataReader?如何从 DataReader 加载列数据?
【发布时间】:2015-11-19 20:04:15
【问题描述】:

我正在使用 EF7 将应用程序移植到 ASP.Net 5.0 并发现几个问题。问题之一是 MS 删除了 DataTable。我正在尝试传输一些代码以不使用 DataTable 而是从 SQLDataReader 读取并将其记录到我拥有的实体中。我必须按列读取数据,但看起来 datareader 只能读取一次。

旧代码:

           Series[] arrSeries = new Series[dt.Columns.Count - 1];

                IList<Categories> arrCats = new List<Categories>();
                Categories arrCat = new Categories();


                foreach (DataColumn dc in dt.Columns)
                {
                    var strarr = dt.Rows.Cast<DataRow>().Select(row => row[dc.Ordinal]).ToList();
                    if (dc.Ordinal == 0)
                    {
                        arrCat.category = strarr.Select(o => new Category { label = o.ToString() }).ToList();
                    }
                    else
                    {
                        Series s = new Series()
                        {
                            seriesname = dc.ColumnName,
                            renderas = null,
                            showvalues = false,
                            data = strarr.Select(o => new SeriesValue { value = o.ToString() }).ToList()
                        };

                        arrSeries[dc.Ordinal - 1] = s;
                    }
                }

                arrCats.Add(arrCat);

                MultiFusionChart fusChart = new MultiFusionChart
                {
                    chart = dictAtts,
                    categories = arrCats,
                    dataset = arrSeries
                };
                return fusChart;

新代码:

                    Series[] arrSeries = new Series[colColl.Count - 1];

                    IList<Categories> arrCats = new List<Categories>();
                    Categories arrCat = new Categories();
                    arrCat.category = new List<Category>();

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        Series s = new Series()
                        {
                            seriesname = reader.GetName(i),
                            renderas = null,
                            showvalues = false
                        };

                        while (reader.Read())
                        {
                            if (i == 0)
                            {
                                Category cat = new Category();
                                cat.label = reader.GetValue(i).ToString();
                                arrCat.category.Add(cat);
                            }
                            else
                            {

                                SeriesValue sv = new SeriesValue();
                                sv.value = reader.GetValue(i).ToString();
                                s.data.Add(sv);

                                arrSeries[i - 1] = s;
                            }
                        }

                    }

                    arrCats.Add(arrCat);

                    MultiFusionChart fusChart = new MultiFusionChart
                    {
                        chart = dictAtts,
                        categories = arrCats,
                        dataset = arrSeries
                    };
                    return fusChart;

在代码工作的地方,它为 Series 返回 null。我相信这是因为读者在记录类别时走到了最后。据我所知,无法重置 DataReader?

有没有办法将列数据从 DataReader 加载到 List 中?或者也许有其他方法可以在这个例子中替换 DataTable?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以像这样指定index,从多个columns 中读取value

    int totalColumns = reader.FieldCount;
    for(int i=0;i<totalColumns;i++)
    {
     var label = reader.GetValue(i);  
    }
    

    您可以通过遍历列来选择所有列的值。

    GetValueint 视为argument,这是列的index,而不是index of row

    【讨论】:

    • 问题是我永远不知道我会有多少列。这就是为什么我正在寻找从一列中获取所有值的解决方案,然后从另一列中获取所有值,等等......
    • 如果对您有帮助,请立即查看答案。
    【解决方案2】:

    问题出在你的 for 循环中。起初,当你的 i 为 0 时,你已经初始化了一个 'Series` 对象 S。之后,

    while (reader.Read())
    

    将阅读您的整个阅读器,而您的 i 仍然为零。所以只有

    if(i == 0)
    

    condition 将返回 true,并且您的整个阅读器都将被花掉。之后,对于 i >= 1,

    while (reader.Read())
    

    将始终返回 false 保持您的数组 arrSeries 为空白。去掉while循环,直接使用索引从dataReader中读取值,

    reader.GetValue(i); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      相关资源
      最近更新 更多