Question

I was advised here [Is this the right way to query a SQL Server CE table for a record, populating and returning a custom object? to use TableDirect for my SQL Server CE queries, and so I have, like so:

cmd.CommandType = CommandType.TableDirect;
cmd.CommandText = "InventoryItems";

...but now I'm wondering if TableDirect is only possible to use with CommandText as a table name, IOW a "Select * From"; would it be possible to do this:

cmd.CommandType = CommandType.TableDirect;
cmd.CommandText = "Select this, that From InventoryItems where Whatever > 42";

?

Was it helpful?

Solution

TableDirect opens a single Table (or index) and has access to all Columns in that Table (or index). You cannot use a query.

You can, however, mimic a lot of query-like things. For example, if you wanted WHERE MyField > 42 you would:

  1. Create an ascending Index on MyField (which you should have whether doing TableDirect or not)
  2. Open that Index with TableDirect (pass the index name in to the Command instad of the Table name).
  3. Use Seek to seek to the first row after "MyField == 42"
  4. Party on with while (rs.Read()) because every row will meet your criteria (you skipped all that didn't).

You'll find that in many cases perf is way, way, WAY better by hand-coding these types of things and not using the query parser. I've dropped the time for running some reports on embedded devices from > 5 minutes to < 15 seconds by creating caching of lookup tables and using table-direct. JOIN == "crappy perf" on an embedded device.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top