I am relatively new to C# and am developing an application that communicates with a local database (SQL Compact 3.5). I am fine with running standard select statements – but when it comes to getting that data into C# – I get a bit lost.
What I have so far is a ‘delete’ query that deletes all rows named ‘Master’.
Now I have worked out that I actually need to get the IDs of those rows before I delete them (for database integrity purposes). I have no problems running a standard select query – my problem is getting a selection of rows from SQL CE into a C# application (using arrays or datatables or whatever is most logical/convenient).
This is what I have at the moment, but it only returns one value, not a selection:
string sql = "select listid from list where ShortDesc='Master'";
SqlCeCommand cmdGetOldMasterId = new SqlCeCommand(sql, DbConnection.ceConnection);
int oldKey = (int)cmdGetOldMasterId.ExecuteScalar();
Console.WriteLine("Old ID: " + oldKey);
I need to be able to do perform a foreach{ } loop on each of the returned rows. Any ideas how I can do this in C#?
You need to use a SqlCeDataReader instead of a ExecuteScalar if you suppose that your sql statement returns more thant one row of data
another possibility is to use a SqlCeDataAdapter to fill a DataTable (this is less performant, but more useful if you need to process your data later in a different method)