SQL Server SqlDataAdapter.Fill(DataTable) Returns No Rows

l2osamch  于 2023-11-16  发布在  其他
关注(0)|答案(7)|浏览(104)

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;
y3bcpkx1

y3bcpkx11#

  1. Try removing the date type parameter in where clause and test again.
  2. And I faced such problem With typed dataset where "Over" not work(IMO), so I guess it will not work here too. Use SPs instead if possible.
rfbsl7qr

rfbsl7qr2#

One thing you could try might be to run ExecuteReader and iterate through the results.

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 (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.

  1. 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 ...
  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 (e.g. parameter name misspelt?), if not, post the code here.
btxsgosb

btxsgosb3#

I was just having a similar problem with 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.

zour9fqk

zour9fqk4#

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.

new9mtju

new9mtju5#

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.

kcugc4gi

kcugc4gi6#

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):

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. 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.

polhcujo

polhcujo7#

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, whereas da.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 :)

相关问题