.NET DataTable中跳过的装入行(DataReader的) [英] .NET DataTable skips rows on Load(DataReader)

查看:292
本文介绍了.NET DataTable中跳过的装入行(DataReader的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图填充一个DataTable,构建一个LocalReport,使用以下:

I'm trying to populate a DataTable, to build a LocalReport, using the following:

MySqlCommand cmd = new MySqlCommand();
cmd.Connection = new MySqlConnection(Properties.Settings.Default.dbConnectionString);
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT ... LEFT JOIN ... WHERE ..."; /* query snipped */

// prepare data
dataTable.Clear();
cn.Open();
// fill datatable
dt.Load(cmd.ExecuteReader());
// fill report
rds = new ReportDataSource("InvoicesDataSet_InvoiceTable",dt);
reportViewerLocal.LocalReport.DataSources.Clear();
reportViewerLocal.LocalReport.DataSources.Add(rds);

在一个点上,我注意到,该报告是不完整的,它缺少一个记录。我已经改变了一些条件,使查询将只返回两行... 惊喜:该报告显示只有一排而不是两个。我试图对其进行调试,以找出问题所在,我被困在

At one point I noticed that the report was incomplete and it was missing one record. I've changed a few conditions so that the query would return exactly two rows and... surprise: The report shows only one row instead of two. I've tried to debug it to find where the problem is and I got stuck at

 dt.Load(cmd.ExecuteReader());

当我注意到,在的DataReader 包含两个记录,但数据表只有一个包含。无意间,我已经添加了一个 ORDER BY 子句来查询,发现这一次的报告显示正确。

显然,DataReader中所包含两行,但数据表只读取他们两个人如果SQL查询字符串包含 ORDER BY (否则只读取最后一个)。任何人都可以解释为什么这种情况正在发生以及如何修复?

When I've noticed that the DataReader contains two records but the DataTable contains only one. By accident, I've added an ORDER BY clause to the query and noticed that this time the report showed correctly.

Apparently, the DataReader contains two rows but the DataTable only reads both of them if the SQL query string contains an ORDER BY (otherwise it only reads the last one). Can anyone explain why this is happening and how it can be fixed?

编辑: 当我第一次贴的问题,我说,这是跳过第一行;后来我意识到,它实际上只看过最后一排,我已经相应地编辑文本(当时所有的记录都分为两行和它似乎跳过前,当它实际上只显示最后一个)。这可能是由一个事实,即它没有由由MySQL所以添加 ORDER BY 返回的行来区分说法引起它来创建一个唯一的标识符引起唯一标识符的每一行。
这只是一个理论,我没有什么可支持它,但我所有的测试似乎导致同样的结果。

When I first posted the question, I said it was skipping the first row; later I realized that it actually only read the last row and I've edited the text accordingly (at that time all the records were grouped in two rows and it appeared to skip the first when it actually only showed the last). This may be caused by the fact that it didn't have a unique identifier by which to distinguish between the rows returned by MySQL so adding the ORDER BY statement caused it to create a unique identifier for each row.
This is just a theory and I have nothing to support it, but all my tests seem to lead to the same result.

推荐答案

我有同样的问题。我从你的博客提示并提出了ORDER BY子句中的查询,以便他们能够共同形成了独特的键全部由查询返回的记录。它解决了这个问题。有点儿怪怪的。

I had same issue. I took hint from your blog and put up the ORDER BY clause in the query so that they could form together the unique key for all the records returned by query. It solved the problem. Kinda weird.

这篇关于.NET DataTable中跳过的装入行(DataReader的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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