开发者

SqlCe odd results why? -- Same SQL, different results in different apps. Issue with

开发者 https://www.devze.com 2022-12-15 13:33 出处:网络
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 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 开发者_JAVA百科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!


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消