views:

65

answers:

2

When I run this SQl in my mobile app I get zero rows.

select * from inventory WHERE [ITEMNUM] LIKE 'PUMP%' AND [LOCATION] = 'GARAGE'

When I run the same SQL in Query Analyzer 3.5 using the same database I get my expected one row.

Why the difference?

Here is the code I'm using in the mobile app:

SqlCeCommand cmd = new SqlCeCommand(Query);
cmd.Connection = new SqlCeConnection("Data Source="+filePath+";Persist Security Info=False;");
DataTable tmpTable = new DataTable();
cmd.Connection.Open();
SqlCeDataReader tmpRdr = cmd.ExecuteReader();
if (tmpRdr.Read())
   tmpTable.Load(tmpRdr);
tmpRdr.Close();
cmd.Connection.Close();
return tmpTable;

UPDATE: For the sake of trying I used the code found in one of the answers found here and it works as expected. So my code looks like this:

SqlCeConnection conn = new SqlCeConnection("Data Source=" + filePath + ";Persist Security Info=False;");
DataTable tmpTable = new DataTable();
SqlCeDataAdapter AD = new SqlCeDataAdapter(Query, conn);
AD.Fill(tmpTable);

The issue appears to be with the SqlCeDataReader.

Hope this helps someone else out!

+1  A: 

Could it be the check you use to see if there are rows in your result set?

The method tmpRdr.Read() will move the cursor to the first row in the result set if there are any rows. Then when you do tmpTable.Load it may be that the data table is loaded from that location and onwards, resulting in the first (and only?) row being skipped.

You can easily check this by removing the line if (tmpRdr.Read()), for it isn't necessary here. The Load method will not fail even if the result set is empty, and you end up with an empty data table in both cases. The only difference will be that you will get the columns set up if you remove the if statement.

Rune Grimstad
That would make perfect sense with the results I was getting... Or rather not getting. I will give that a go just to see what happens.
NitroxDM
That was it. Thanks!
NitroxDM
A: 

I agree with Rune - you should omit

if (tmpRdr.Read())

In case you absolutely want to check if you get a row as a result of execution, you'd better use this code:

if (tmpRdr.HasRows)
AlexS