.NET DataTable 在 Load(DataReader) 上跳过行 [英] .NET DataTable skips rows on Load(DataReader)

查看:21
本文介绍了.NET DataTable 在 Load(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 包含两条记录而 DataTable 只包含一条记录时.偶然地,我在查询中添加了一个 ORDER BY 子句,并注意到这次报告显示正确.

显然,DataReader 包含两行,但如果 SQL 查询字符串包含 ORDER BY(否则它只读取最后一行),DataTable 只会读取它们.谁能解释为什么会发生这种情况以及如何解决?

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 在 Load(DataReader) 上跳过行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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