简体   繁体   English

SqlDataAdapter.Fill(DataTable)不返回任何行

[英]SqlDataAdapter.Fill(DataTable) Returns No Rows

I am trying to use a SqlDataAdapter to fill a DataTable, which I use as a data source to a DataGrid. 我正在尝试使用SqlDataAdapter填充DataTable,并将其用作DataGrid的数据源。 The query is fine, at least when I run it manually in SSMSE. 该查询很好,至少当我在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): 更新 (由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;
  1. Try removing the date type parameter in where clause and test again. 尝试删除where子句中的日期类型参数,然后再次进行测试。
  2. And I faced such problem With typed dataset where "Over" not work(IMO), so I guess it will not work here too. 我遇到了这样的问题,即类型数据集的“ Over”不起作用(IMO),因此我猜想它在这里也不会起作用。 Use SPs instead if possible. 如果可能,请改用SP。

One thing you could try might be to run ExecuteReader and iterate through the results. 您可以尝试的一件事是运行ExecuteReader并遍历结果。

IDataReader reader = m_areaDataAdapter.SelectCommand.ExecuteReader();
while(reader.Read())
{
    ...
}

This might help you to see if the problem is with your command, or with your DataTable. 这可以帮助您查看问题是否出在您的命令或数据表上。

I know the parameters are being added correctly, I know that data exists for the given query 我知道参数已正确添加,我知道给定查询存在数据

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 (eg ANSI NULLS, ANSI PADDING) and therefore give different results. 还要注意,当使用SSMSE手动执行查询时,您的连接配置可能会有所不同(例如ANSI NULLS,ANSI PADDING),因此会产生不同的结果。

EDIT 编辑

In response to Nik's comment, here are some suggestions for debugging this. 为了回应Nik的评论,以下是一些调试建议。

  1. Run similar code, but modifying the query so that it does not have any parameters (ie 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. 如果这不起作用,则似乎您的连接配置可能与SSMSE不同。 If it does work, then ... 如果有效,那么...

  2. 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 (eg parameter name misspelt?), if not, post the code here. 一分钱可能会减少,您会明白为什么(例如,参数名称misspelt?),如果没有,请在此处发布代码。

I was just having a similar problem with OracleDataAdapter . 我只是在OracleDataAdapter遇到类似的问题。 I was trying to even do a simple 我甚至试图做一个简单的

select * 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. 不过,我仍然很困惑,因为我以前从未必须设置这些属性。

I had a similar issue, though I don't know if it addresses the OP's question, but may help someone: 我有一个类似的问题,尽管我不知道它是否解决了OP的问题,但可能会帮助某人:

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). 我遇到一个问题,我没有返回任何行,并且出现异常Cannot find table 0 ,使用类似的代码来使用适配器填充数据集(这会在数据集中自动创建一个表)。

Relevant snippet of code (doesn't show OracleCommand, connection opening, for simplicity): 相关代码段(为简单起见,不显示OracleCommand,打开连接):

DataSet ds = new DataSet();
OracleDataAdapter oraAdp = new OracleDataAdapter();

oraAdp.Fill(ds);
return ds.Tables[0];

I knew the query I was sending via an OracleCommand should have done the fill and gotten results - or so I thought. 我知道我通过OracleCommand发送的查询应该已经完成​​了填充并得到了结果-大概是这样。 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. 当我在Oracle SQL Developer中运行查询时,我得到了真正的例外,那就是我对一个字段的查询中的字段名称错误。 When I corrected my query, all came through fine. 当我更正查询时,一切顺利。

Are you accessing the query through a webservice that executes the ADO.NET call? 您是否正在通过执行ADO.NET调用的Web服务访问查询? If the server's timezone is different, the values for @StartDate and @EndDate may not be what you expect. 如果服务器的时区不同,则@StartDate和@EndDate的值可能不是您所期望的。

There may be problem between your join by Multiple WHERE Clause. 您在通过多个WHERE子句进行的加入之间可能存在问题。 You have to find out that whatever table are join on specific ID is correct or not as per your database table and data. 您必须根据数据库表和数据来确定在特定ID上联接的表是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM