SqlDataAdapter.Fill(DataTable) 不返回任何行
我正在尝试使用 SqlDataAdapter
来填充 DataTable
,我将其用作 DataGrid
的数据源。查询很好,至少当我在 SSMSE 中手动运行它时是这样。当我执行填充操作时,表获得正确的列,但没有行。最令人沮丧的部分是不同页面上的代码是相同的(具有不同的查询)。我无法发布查询,但我的初始化代码如下所示:
SqlCommand areaDAC = new SqlCommand (areaQuery, connection);
areaDAC.Parameters.Add (new SqlParameter ("@param",
System.Data.SqlDbType.NVarChar, 50));
m_areaDataAdapter = new SqlDataAdapter (areaDAC);
然后使用它:
m_areaDataAdapter.SelectCommand.Parameters["@param"].Value = "Filter Val";
DataTable table = new DataTable ();
m_areaDataAdapter.Fill (table);
此时,表具有正确的列数并且没有行。我知道参数已正确添加,我知道给定查询存在数据。
更新(由 Nik 在评论中提供):
SELECT * FROM
(SELECT
ROW_NUMBER() OVER(ORDER BY DateAndTime DESC) AS rowNum,
areaName, stationName, lineName, DateAndTime,
Element, Description
FROM
sdrReportArea, sdrReportStation, sdrReportTLine,
SDRSequenceEvents, SDRSequenceStates
WHERE
sdrReportArea.areaID = sdrReportStation.stationID
AND sdrReportStation.stationID = sdrReportTLine.stationID
AND sdrReportTLine.lineID = SDRSequenceEvents.LineID
AND SDRSequenceEvents.StateID = SDRSequenceStates.StateID
AND DateAndTime >= @startDate AND DateAndTime <= @endDate
AND areaName = @area) AS TempTbl
WHERE
rowNum BETWEEN @startRow AND @endRow;
I am trying to use a SqlDataAdapter
to fill a DataTable
, which I use as a data source to a DataGrid
. The query is fine, at least when I run it manually in SSMSE. When I do the fill operation, the table gets the right columns, but no rows. The most frustrating part of this is that the code is identical (with a different query) on a different page. I cannot post the query, but my initialization code looks like this:
SqlCommand areaDAC = new SqlCommand (areaQuery, connection);
areaDAC.Parameters.Add (new SqlParameter ("@param",
System.Data.SqlDbType.NVarChar, 50));
m_areaDataAdapter = new SqlDataAdapter (areaDAC);
Then to use it:
m_areaDataAdapter.SelectCommand.Parameters["@param"].Value = "Filter Val";
DataTable table = new DataTable ();
m_areaDataAdapter.Fill (table);
At this point, table has the right number of columns and no rows. I know the parameters are being added correctly, I know that data exists for the given query.
Update (as provided by Nik in a comment):
SELECT * FROM
(SELECT
ROW_NUMBER() OVER(ORDER BY DateAndTime DESC) AS rowNum,
areaName, stationName, lineName, DateAndTime,
Element, Description
FROM
sdrReportArea, sdrReportStation, sdrReportTLine,
SDRSequenceEvents, SDRSequenceStates
WHERE
sdrReportArea.areaID = sdrReportStation.stationID
AND sdrReportStation.stationID = sdrReportTLine.stationID
AND sdrReportTLine.lineID = SDRSequenceEvents.LineID
AND SDRSequenceEvents.StateID = SDRSequenceStates.StateID
AND DateAndTime >= @startDate AND DateAndTime <= @endDate
AND areaName = @area) AS TempTbl
WHERE
rowNum BETWEEN @startRow AND @endRow;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您是否通过执行 ADO.NET 调用的 Web 服务访问查询?如果服务器的时区不同,@StartDate 和@EndDate 的值可能不是您所期望的。
Are you accessing the query through a webservice that executes the ADO.NET call? If the server's timezone is different, the values for @StartDate and @EndDate may not be what you expect.
您通过多个 WHERE 子句进行的连接之间可能存在问题。您必须根据您的数据库表和数据确定在特定 ID 上连接的任何表是否正确。
There may be problem between your join by Multiple WHERE Clause. You have to find out that whatever table are join on specific ID is correct or not as per your database table and data.
我有一个类似的问题,虽然我不知道它是否解决了OP的问题,但可能会帮助某人:
我有一个问题,我没有返回任何行,并且出现异常
找不到表0
,使用类似的代码使用适配器填充数据集(这会自动在数据集中创建一个表)。相关代码片段(为了简单起见,不显示 OracleCommand、连接打开):
我知道我通过 OracleCommand 发送的查询应该已经完成填充并获得结果 - 或者我是这么认为的。当我在 Oracle SQL Developer 中运行查询时,我遇到了真正的异常,即我的查询中某个字段的字段名称错误。当我更正我的查询时,一切顺利。
I had a similar issue, though I don't know if it addresses the OP's question, but may help someone:
I had an issue where I was getting no rows returned and an exception
Cannot find table 0
, using similar code to use the adapter to fill a dataset (which automatically creates a table in the dataset).Relevant snippet of code (doesn't show OracleCommand, connection opening, for simplicity):
I knew the query I was sending via an OracleCommand should have done the fill and gotten results - or so I thought. When I ran the query in Oracle SQL Developer, I got the real exception, which was that I had the wrong field name in my query for one of the fields. When I corrected my query, all came through fine.
12 年后,当 sql 执行几个步骤并在最后执行选择时,我遇到了问题。
如果您执行 da.Fill(dataset) ,它会起作用,而 da.Fill(datatable) 则不起作用。
就我而言,sql 通过 select..into 创建了一个临时表,然后进行了一些更新,最后执行了一条 select 语句以在最后返回数据。这不能直接应用于数据表,但它可以很好地应用于数据集。
希望对某人有帮助:)
12 years later but I hit the problem when the sql does several steps and does a select at the end.
If you do
da.Fill(dataset)
it works, whereasda.Fill(datatable)
did not work.In my case the sql created a temp table via select..into, then it does a few updates, then it finally does a select statement to return the data at the end. This did not work directly into a datatable, but it works fine into a dataset.
Hope that helps someone :)
您可以尝试的一件事可能是运行 ExecuteReader 并迭代结果。
这可能会帮助您查看问题是否出在您的命令或数据表上。
当您进行调试时,您应该质疑所有假设,包括这一假设。另请注意,使用 SSMSE 手动执行查询时,您的连接可能配置不同(例如 ANSI NULLS、ANSI PADDING),因此会给出不同的结果。
编辑
为了回应 Nik 的评论,这里有一些调试的建议。
运行类似的代码,但修改查询,使其不具有任何参数(即开始/结束日期、区域、开始/结束行的硬连线值)。如果这不起作用,那么您的连接配置可能与 SSMSE 不同。如果它确实有效,那么...
将参数一一添加到您的查询中,直到失败。然后您将能够确定哪个参数导致其失败。可能一分钱都会掉下来,您会明白为什么(例如参数名称拼写错误?),如果没有,请在此处发布代码。
One thing you could try might be to run ExecuteReader and iterate through the results.
This might help you to see if the problem is with your command, or with your DataTable.
When you're debugging, you should question all your assumptions, including this one. Note also that when executing queries manually using SSMSE, your connection may be configured differently (e.g. ANSI NULLS, ANSI PADDING) and therefore give different results.
EDIT
In response to Nik's comment, here are some suggestions for debugging this.
Run similar code, but modifying the query so that it does not have any parameters (i.e. hardwired values for start/end date, area, start/end row). If this doesn't work, then it seems likely that your connection may be configured differently from SSMSE. If it does work, then ...
Add parameters to your query one by one until it fails. You will then be able to identify which parameter caused it to fail. Likely the penny will drop and you'll see why (e.g. parameter name misspelt?), if not, post the code here.
我刚刚在 OracleDataAdapter 上遇到了类似的问题。我什至尝试做一个简单的
select * from table
但它不起作用。我最终设置了
DbCommand.CommandTimeout
属性,它开始工作。但我仍然很困惑,因为我以前从未需要设置这些属性。
I was just having a similar problem with
OracleDataAdapter
. I was trying to even do a simpleselect * from table
and it wasn't working. I ended up setting the
DbCommand.CommandTimeout
property and it began to work. I'm still confused, though, because I have previously never had to set these properties.